Mar*_*ons 2 javascript regex string replace match
我试图从给定的字符串中获得精确匹配,然后操作该字符串。我有一个相当大的计算器程序,你可以在这里看到:http : //www.marcusparsons.com/projects/calculator。主要页面上还没有任何内容,原始代码很长。
目标是在计算器中实现一个功能,其中 Math 不必以 Math 对象/方法为前缀。在我添加了一个允许用户使用“acosh()”方法(和实验方法)的函数之前,它一直工作得很好,无论它是否在他们的浏览器中实现(呃……IE)。我遇到的问题是我现在的算法想用 aMath.cosh() 替换“acosh”,因为它在“acosh”中看到“cos”。
因此,当我将字符串“acosh(1)+cos(pi/3)”传递给它时,它会变成“aMath.cosh(1)+cos(Math.PI/3)”。
编辑:上面的字符串应该是“acosh(1)+Math.cos(Math.PI/3)”。
我是正则表达式的新手,我认为这就是我的问题所在。
这是示例代码:http : //jsfiddle.net/mparson8/2ej5n3u4/4/
var $mathKeywords = ["E", "LN2", "LN10", "LOG2E", "LOG10E", "PI", "SQRT1_2", "SQRT2", "abs", "acos", "asin", "asinh", "atan", "atan2", "atanh", "cbrt", "ceil", "clz32", "cos", "exp", "expm1", "floor", "fround", "hypot", "imul", "log1p", "log10", "log2", "max", "min", "pow", "random", "round", "sin", "sinh", "sqrt", "tan", "tanh", "trunc"];
var $resultVal = "acosh(1)+cos(PI/3)".toLowerCase();
try {
//Iterate over each Math object/method
$.each($mathKeywords, function (i, val) {
//Convert val within array to a lower case form
var $lowerKey = val.toLowerCase();
//The regex pattern I came up with
var pattern = new RegExp("(^|\\W)" + $lowerKey + "($|\\W)");
//See if pattern gives a match within $resultVal
var $location = $resultVal.match(pattern);
//Math keyword is found
if ($location != null) {
//replace the lowercase version of the math keyword with its properly cased version prepended
//with Math. i.e. cos becomes Math.cos and pi becomes Math.PI
$resultVal = $resultVal.replace($lowerKey, "Math." + val);
}
});
//Set the result element's value to an evaluation of $resultVal
//A better implementation of the eval exists within the calc program
alert($resultVal);
alert(eval($resultVal));
} catch (err) {
alert("Error: Cannot process expression due to " + err + ".");
}
Run Code Online (Sandbox Code Playgroud)
我感谢任何和所有的帮助!:)
如果你想继续正则表达式的路径,这似乎有效:
var $mathKeywords = ["E", "LN2", "LN10", "LOG2E", "LOG10E", "PI", "SQRT1_2", "SQRT2", "abs", "acos", "asin", "asinh", "atan", "atan2", "atanh", "cbrt", "ceil", "clz32", "cos", "exp", "expm1", "floor", "fround", "hypot", "imul", "log1p", "log10", "log2", "max", "min", "pow", "random", "round", "sin", "sinh", "sqrt", "tan", "tanh", "trunc"];
var $resultVal = "acosh(1)+cos(PI/3)".toLowerCase();
try {
//Iterate over each Math object/method
$.each($mathKeywords, function (i, val) {
//Convert val within array to a lower case form
var $lowerKey = val.toLowerCase();
var pattern = new RegExp("\\b" + $lowerKey + "\\b", "g");
//See if pattern gives a match within $resultVal
var $location = $resultVal.match(pattern);
//Math keyword is found
if ($location != null) {
//replace the lowercase version of the math keyword with its properly cased version prepended
//with Math. i.e. cos becomes Math.cos and pi becomes Math.PI
$resultVal = $resultVal.replace(pattern, "Math." + val);
}
});
//Set the result element's value to an evaluation of $resultVal
//A better implementation of the eval exists within the calc program
console.log($resultVal);
console.log(eval($resultVal));
} catch (err) {
alert("Error: Cannot process expression due to " + err + ".");
}
Run Code Online (Sandbox Code Playgroud)
输出:
acosh(1)+Math.cos(Math.PI/3)
Run Code Online (Sandbox Code Playgroud)
(实际上是它,Error: Cannot process expression due to ReferenceError: acosh is not defined.但你明白了)
变化:
\b作词边界g标志替换所有出现。