[英]Get exact match in string using regex and javascript
我試圖從給定的字符串中獲得完全匹配的內容,然后操縱該字符串。 我有一個相當大的計算器程序,您可以在這里看到: 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
標志替換所有出現的事件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.