Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用正则表达式和javascript获得字符串中的精确匹配_Javascript_Regex_String_Replace_Match - Fatal编程技术网

使用正则表达式和javascript获得字符串中的精确匹配

使用正则表达式和javascript获得字符串中的精确匹配,javascript,regex,string,replace,match,Javascript,Regex,String,Replace,Match,我试图从给定的字符串中获得一个精确的匹配,然后操纵该字符串。我有一个相当大的计算器程序,你可以在这里看到:。主页上还没有任何内容,原始代码相当长 我们的目标是在计算器中实现一个特性,在这个特性中,数学对象/方法不必以数学为前缀。直到我添加了一个函数,允许用户使用“acosh()”方法(和实验方法),不管它是否在他们的浏览器中实现(ehem…IE),它一直工作得很好。我遇到的问题是,我现在的算法想要用aMath.cosh()替换“acosh”,因为它在“acosh”中看到“cos” 因此,当我传递

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

我们的目标是在计算器中实现一个特性,在这个特性中,数学对象/方法不必以数学为前缀。直到我添加了一个函数,允许用户使用“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)”

我是正则表达式的新手,我想这就是我的问题所在

下面是示例代码:


非常感谢您的帮助!:)

如果您想继续使用正则表达式,这似乎是可行的:

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)
(实际上它的
错误:由于ReferenceError:acosh没有定义,所以无法处理表达式。
但你明白了)


变化:

  • 使用
    \b
    作为单词边界
  • 替换时使用模式(带单词边界)
  • 使用
    g
    标志替换所有引用

你的列表中acosh在哪里?
(^ |\W)cos($|\W)
不匹配
acosh
;你已经正确添加了单词边界,所以我不确定这是你的问题……acosh不属于列表,因为它将是数学原型的一部分,如果你尝试运行“acosh”“在IE中,您将得到一个错误,因为它不作为数学方法的一部分存在。我还没有拿出其他实验方法,因为我刚刚运行了测试。马克,正则表达式与代码返回的不一样。如果在设置$location后显示$location的警报
$resultVal.match(模式)您将看到它还返回了一些额外的字符。@MarcusParsons噢。。。这是因为
\W
与标点/运算符(例如)匹配。您需要的是零宽度断言或捕获组。只需将
(^ |\\W)
($|\\W)
都替换为
\b
(或字符串中的
\\b
),例如,不。您仍然会遇到我上面描述的问题。重新排列列表,我认为我们可能会遇到问题winner@deweyredman你的回答不正确。单词boundary
\b
处理这个问题
\bcos\b
将不匹配
.acos
(或
.acosh
)。Halcyon,这正是我需要的。我的问题是正则表达式和我需要替换的内容。我觉得这太无聊了!片刻。非常感谢!:)这确实是一个很好的答案。这一行需要突出显示:
newregexp(“\\b”+$lowerKey+”\\b,“g”)
acosh(1)+Math.cos(Math.PI/3)