gpt4 book ai didi

javascript - 使用正则表达式和 javascript 在字符串中获取完全匹配

转载 作者:行者123 更新时间:2023-11-29 18:09:44 25 4
gpt4 key购买 nike

我试图从给定的字符串中获得精确匹配,然后操作该字符串。我有一个相当大的计算器程序,您可以在这里看到:http://www.marcusparsons.com/projects/calculator .主页上还没有任何内容,原始代码很长。

目标是在计算器中实现一项功能,其中 Math 不必作为 Math 对象/方法的前缀。在我添加一个允许用户使用“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 标志替换所有 出现的地方。

关于javascript - 使用正则表达式和 javascript 在字符串中获取完全匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28596211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com