2010-06-16 73 views
0

我正在写一段客户端JavaScript代码,它需要一个函数并找到它的派生词,但是,应该匹配的正则表达式权力规则无法在JavaScript程序的上下文中工作,即使它与独立正则表达式测试程序一起使用时也能成功匹配。JS正则表达式不匹配,甚至认为它与正则表达式测试仪一起工作

此时的代码非常简陋,而且我打算以后再用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

请特别说明您遇到麻烦的字符串和模式。它看起来像你有很多,我不愿意通过并修复整个程序,但我可以轻松地帮助你一个字符串。 – 2010-06-16 16:14:19

+0

我确定了我遇到麻烦的那种模式(应该与动力规则匹配的模式)。但是,由于某些用户可能不熟悉它是什么或应该是什么样子,我已经进一步澄清。 – 2010-06-16 18:28:41

+0

编辑:其实,有一个小的数学错误,所以我很抱歉我造成的任何困惑。 – 2010-06-16 18:43:47

回答

2

您需要双击逃避你插入符:

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

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