使用正则表达式和 javascript 在字符串中获取精确匹配
Get exact match in string using regex and javascript
我试图从给定的字符串中获取精确匹配,然后处理该字符串。我有一个相当大的计算器程序,您可以在这里看到:http://www.marcusparsons.com/projects/calculator。主页上还没有任何内容,原始代码很长。
我们的目标是在计算器中实现一项功能,其中 Math 不必在 Math objects/methods 前加上前缀。在我添加一个允许用户使用 "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
标志替换 所有 次出现。
我试图从给定的字符串中获取精确匹配,然后处理该字符串。我有一个相当大的计算器程序,您可以在这里看到:http://www.marcusparsons.com/projects/calculator。主页上还没有任何内容,原始代码很长。
我们的目标是在计算器中实现一项功能,其中 Math 不必在 Math objects/methods 前加上前缀。在我添加一个允许用户使用 "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
标志替换 所有 次出现。