我試圖用JS替換一些URL。 我不明白,爲什麼正則表達式在這裏不匹配。 我在這個網站上測試了我的表情:http://www.regular-expressions.info/javascriptexample.html 我在這裏得到了積極的結果,但沒有在我自己的腳本中。 任何人都可以在這裏協助嗎?將var傳遞給正則表達式與URL不匹配
var pattern = new RegExp("http://www\.example\.com/out/\?url=","g");
var context = "http://www.example.com/out/?url=http://google.com";
if(context.match(pattern))
{
context = context.replace(pattern,"");
alert(context);
}
else
alert("no match");
使用'new RegExp'時需要雙重轉義。我猜測你在測試正則表達式文字的網站上。即新的RegExp(「http:// www \\ .example \\ .com/out/\\?url =」,「g」)' – Xotic750
http://jsfiddle.net/Xotic750/FA64L/ – Xotic750
請參閱[ 'RegExp'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions) – Xotic750