我试图从给定的字符串中获得完全匹配的内容,然后操纵该字符串。我有一个相当大的计算器程序,您可以在这里看到:http : //www.marcusparsons.com/projects/calculator。主页上还没有任何内容,并且原始代码很长。
目标是在计算器中实现一个功能,其中不必在数学对象/方法之前添加数学前缀。在我添加了一个允许用户使用“ acosh()”方法(和实验方法)的功能之前,无论它是否在浏览器中实现(ehem ... 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 + ".");
}
我感谢所有帮助!:)
如果要继续使用正则表达式,这似乎可行:
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 + ".");
}
输出:
acosh(1)+Math.cos(Math.PI/3)
(实际上是,Error: Cannot process expression due to ReferenceError: acosh is not defined.
但您明白了)
变化:
\b
的单词边界g
标志替换所有出现的事件。本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句