English 中文(简体)
JS regex isn t matching, even thought it works with a regex tester
原标题:

I m writing a piece of client-side javascript code that takes a function and finds the derivative of it, however, the regex that s supposed to match with the power rule fails to work in the context of the javascript program, even though it sucessfully matches when it s used with an independent regex tester.

The code is extremely bare-bones at the moment, and I m planning to flesh it out later.

The browser I m executing this on is Midori, and the operating system is Ubuntu 10.04 (Lucid Lynx). Here s the HTML page being used as the interface in addition to the code:

Page:

<html>
  <head>
    <title>
      Derivative Calculator
    </title>
    <script type="text/javascript" src="derivative.js">
    </script>
    <body>
      <form action="" name=form>
  <input type=text name=f /> with respects to
  <input type=text name=vr size=7 />
  <input type=button value="Derive!" onClick="main(this.form)" /> <br />
  <input type=text name=result value="" />
      </form>
   </body>
</html>

derivative.js:

function main(form) {
    form.result.value = derive(form.f.value, form.vr.value);
}

function derive(f, v) {
    var atom = [];
    atom["sin(" + v + ")"] = "cos(" + v + ")";
    atom["cos(" + v + ")"] = "-sin(" + v + ")";
    atom["tan(" + v + ")"] = "sec^(2)(" + v + ")";
    atom["sec(" + v + ")"] = "sec(" + v + ")*tan(" + v + ")";
    atom["1/(cos(" + v + "))"] = "sec(" + v + ")*tan(" + v + ")";
    atom["csc(" + v + ")"] = "-csc(" + v + ")*cot(" + v + ")";
    atom["1/(sin(" + v + "))"] = "-csc(" + v + ")*cot(" + v + ")";
    atom["cot(" + v + ")"] = "-csc^(2)(" + v + ")";
    atom["1/(tan(" + v + "))"] = "-csc^(2)(" + v + ")";
    atom["sin^(-1)(" + v + ")"] = "1/sqrt(1 - " + v + "^(2))";
    atom["arcsin(" + v + ")"] = "1/sqrt(1 - " + v + "^(2))";
    atom["cos^(-1)(" + v + ")"] = "-1/sqrt(1 - " + v + "^(2))";
    atom["arccos(" + v + ")"] = "-1/sqrt(1 - " + v + "^(2))";
    atom["tan^(-1)(" + v + ")"] = "1/(1 + " + v + "^(2))";
    atom["arctan(" + v + ")"] = "1/(1 + " + v + "^(2))";
    atom["sec^(-1)(" + v + ")"] = "1/(|" + v + "|*sqrt(" + v + "^(2) - 1))"; 
    atom["arcsec(" + v + ")"] = "1/(|" + v + "|*sqrt(" + v + "^(2) - 1))"; 
    atom["csc^(-1)(" + v + ")"] = "-1/(|" + v + "|*sqrt(" + v + "^(2) - 1))"; 
    atom["arccsc(" + v + ")"] = "-1/(|" + v + "|*sqrt(" + v + "^(2) - 1))"; 
    atom["cot^(-1)(" + v + ")"] =  "-1/(1 + " + v + "^(2))";
    atom["arccot(" + v + ")"] =  "-1/(1 + " + v + "^(2))";
    atom["ln(" + v + ")"] = "1/(" + v + ")";
    atom["e^(" + v + ")"] = "e^(" + v + ")";
    atom["ln(|" + v + "|)"] = "1/(" + v + ")";
    atom[v] = "1";

    var match = "";
    if (new Boolean(atom[f]) == true) {
        return atom[f];
    } else if (f.match(/^[0-9]+$/)) {
        return "";
    } else if (f.match(/([S]+)([s]+)+([s]+)([S]+)/)) {
        match = /([S]+)([s]+)+([s]+)([S]+)/.exec(f);
        return derive(match[1], v) + " + " + derive(match[4], v);
    } else if (f.match(new RegExp("^([0-9]+)(" + v + ")$"))) {
        match = new RegExp("^([0-9]+)(" + v + ")$").exec(f);
        return match[1];
    } else if (f.match(new RegExp("^([0-9]+)(" + v + ")^([0-9]+)$"))) {
        match = new RegExp("^([0-9]+)(" + v + ")^([0-9]+)$").exec(f);
        return String((match[1] * (match[3]))) + v + "^" +
                   String(match[3] - 1);
    } else {
        return "?";
    }
}

In particular, I m having trouble with this pattern:

    } else if (f.match(new RegExp("^([0-9]+)(" + v + ")^([0-9]+)$"))) {
        match = new RegExp("^([0-9]+)(" + v + ")^([0-9]+)$").exec(f);
        return String((match[1] * (match[3]))) + v + "^" +
                   String(match[3] - 1);
最佳回答

You need to double-escape your caret:

")\^([0-9]+)$"

Otherwise javascript doesn t interpret the backslash as a backslash, and therefore never escapes the caret for the regex.

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签