test - regular expression validation javascript
你如何在正則表達式中使用變量? (11)
我想在JavaScript中創建一個String.replaceAll()方法,我想使用RegEx是最簡單的方法。 但是,我無法弄清楚如何將一個變量傳遞給RegEx。 我已經可以做到這一點,它將用“A”代替“B”的所有實例。
"ABABAB".replace(/B/g, "A");
但我想要做這樣的事情:
String.prototype.replaceAll = function(replaceThis, withThis) {
this.replace(/replaceThis/g, withThis);
};
但顯然這只會取代文本“replaceThis”...所以如何將這個變量傳遞給我的RegEx字符串?
"ABABAB".replace(/B/g, "A");
一如既往:除非必須,否則不要使用正則表達式。 對於一個簡單的字符串替換,成語是:
'ABABAB'.split('B').join('A')
那麼你不必擔心Gracenotes的答案中提到的引用問題。
不用/regex/g
語法,你可以構建一個新的RegExp對象:
var replace = "regex";
var re = new RegExp(replace,"g");
您可以通過這種方式動態創建正則表達式對象。 那麼你會做:
"mystring".replace(re, "newstring");
和史蒂文彭尼的答案咖啡腳本版本,因為這是#2谷歌結果....即使咖啡只是與很多字符刪除的JavaScript ...;)
baz = "foo"
filter = new RegExp(baz + "d")
"food fight".match(filter)[0] // food
並在我的具體情況
robot.name=hubot
filter = new RegExp(robot.name)
if msg.match.input.match(filter)
console.log "True!"
如果$ 1不適合你,你可以使用它
var pattern = new RegExp("amman","i");
"abc Amman efg".replace(pattern,"<b>"+"abc Amman efg".match(pattern)[0]+"</b>");
您想要動態構建正則表達式,因此正確的解決方案是使用new RegExp(string)
構造函數。 為了使構造函數從字面上處理特殊字符,您必須轉義它們。 jQuery UI自動完成小部件中有一個內置函數,名為$.ui.autocomplete.escapeRegex
:
[...]您可以使用內置的
$.ui.autocomplete.escapeRegex
函數。 它將採用單個字符串參數並轉義所有正則表達式字符,使結果安全地傳遞到new RegExp()
。
如果您使用的是jQuery UI,您可以使用該功能,或者從源代碼複製其定義:
function escapeRegex(value) {
return value.replace( /[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&" );
}
並像這樣使用它:
"[z-a][z-a][z-a]".replace(new RegExp(escapeRegex("[z-a]"), "g"), "[a-z]");
// escapeRegex("[z-a]") -> "\[z\-a\]"
// new RegExp(escapeRegex("[z-a]"), "g") -> /\[z\-a\]/g
// end result -> "[a-z][a-z][a-z]"
所有的解決方案都在這裡,
我已經實現的是從一個文本字段,這是你想要替換和另一個是“替換”文本字段的值,
從一個變量的文本字段中獲取值,並將該變量設置為RegExp函數以進一步替換。在我的例子中,我使用的是Jquery,您也可以只通過javaScript來完成。
JavaScript代碼:
var replace =document.getElementById("replace}"); // getting a value from a text field with I want to replace
var replace_with = document.getElementById("with"); //Getting the value from another text fiels with which I want to replace anather string.
var sRegExInput = new RegExp(replace, "g");
$("body").children().each(function() {
$(this).html($(this).html().replace(sRegExInput,replace_with));
});
這段代碼是一個按鈕的Onclick事件,你可以把它放在一個函數中調用。
所以現在你可以通過替換函數傳遞變量。
為了滿足我需要在正則表達式中插入變量/別名/函數,這就是我想到的:
oldre = /xx\(""\)/;
function newre(e){
return RegExp(e.toString().replace(/\//g,"").replace(/xx/g, yy), "g")
};
String.prototype.replaceAll = this.replace(newre(oldre), "withThis");
其中'oldre'是我想要插入變量的原始正則表達式,'xx'是該變量/別名/函數的佔位符,'yy'是實際的變量名稱,別名或函數。
這個:
var txt=new RegExp(pattern,attributes);
相當於這樣:
var txt=/pattern/attributes;
雖然你可以創建動態創建的RegExp(按照這個問題的其他回答),但我會回复我的評論: String.replace()的功能形式非常有用,並且在很多情況下減少了對動態創建的RegExp對象。 (這是一種痛苦,因為你必須以字符串的形式向RegExp構造函數表示輸入,而不是使用斜線/ [AZ] + / regexp文字格式)
String.prototype.replaceAll = function (replaceThis, withThis) {
var re = new RegExp(replaceThis,"g");
return this.replace(re, withThis);
};
var aa = "abab54..aba".replaceAll("\\.", "v");
使用此tool測試
this.replace( new RegExp( replaceThis, 'g' ), withThis );