使用正则表达式和JavaScript获取字符串中的完全匹配

马库斯·帕森斯(Marcus Parsons)

我试图从给定的字符串中获得完全匹配的内容,然后操纵该字符串。我有一个相当大的计算器程序,您可以在这里看到: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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

正则表达式在括号之间和Javascript中的字符串文字内部获取字符串

正则表达式获取字符串的开始和结束匹配

使用正则表达式获取字符串中的两个匹配项

在VBA中使用正则表达式仅获取字符串中的数字

使用正则表达式获取字符串中模式的索引

如何使用正则表达式仅获取字符串括号中的内容?

如何使用正则表达式从引号中获取字符串

使用javascript正则表达式获取字符串中的最后一个数字

正则表达式:获取字符串的匹配部分和“不匹配”部分

使用正则表达式顺序匹配正则表达式,而不是JavaScript中的字符串顺序

正则表达式从字符串中获取字符串

正则表达式完全匹配字符串?

字符串完全匹配的正则表达式

与正则表达式匹配后获取字符串的其他部分

正则表达式-C#-获取字符串的不匹配部分

.NET正则表达式-匹配后获取字符串

正则表达式匹配并通过模式JAVA获取字符串

正则表达式:如何获取字符串中组的所有匹配项

使用正则表达式从字符串中的提取字符串

不使用NSRegularExpression,如何获取字符串正则表达式的所有匹配项?

使用 Gsub 获取 R 中匹配的字符串 - 正则表达式

使用JavaScript正则表达式匹配和替换字符串

如何在正则表达式中获取字符串并在匹配字符串后删除其他字符串

如何使用正则表达式提取字符串的不匹配部分

如何使用正则表达式从列表中提取字符串匹配项?

使用正则表达式C#获取字符之间的字符串

使用正则表达式获取字符串中的特定字符并删除未使用的零

正则表达式在冒号之前和之后获取字符串

正则表达式在逗号和句号之间获取字符串