javascript - JS 正则表达式不匹配,即使它与正则表达式测试器一起工作

标签 javascript regex math symbolic-math

我正在编写一段客户端 JavaScript 代码,它接受一个函数并找到它的导数,但是,应该与幂规则匹配的正则表达式无法在 JavaScript 程序的上下文中工作,甚至尽管与独立的正则表达式测试器一起使用时它会成功匹配。

目前代码非常简单,我计划 flesh it out稍后。

我执行此操作的浏览器是 Midori,操作系统是 Ubuntu 10.04 (Lucid Lynx)。除了代码之外,下面是用作界面的 HTML 页面:

页面:

<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 "?";
    }
}

特别是,我在使用这种模式时遇到了麻烦:

    } 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);

最佳答案

您需要双重转义插入符号:

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

否则 javascript 不会将反斜杠解释为反斜杠,因此永远不会转义正则表达式的插入符号。

关于javascript - JS 正则表达式不匹配,即使它与正则表达式测试器一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3055129/

相关文章:

javascript - 通过字符串将 html 对象传递给函数

regex - cucumber 正则表达式

java - 如何检查用户输入是否是一个单词而不是像 (*!?) 这样的特殊字符?

c - 在 C 中,关于 %d 如何识别字符串中的变量的技术解释

python - 如何在 python 中四舍五入到更高的 10 位

math - 如何从数字中提取每个数字?

javascript - 使用 jquery 将 &lt;script&gt; 附加到 <div> 中

javascript - 绘制一条总是与其父 BoxElement 一样宽的线?

javascript - 是否可以将 modernizr 添加到 jsFiddle 并在 Chrome/firefox 中查看 css3 透视图

python - 如何用正则表达式匹配整个单词?