2012-03-21 70 views
1
regexp = new RegExp(\b[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}\b); 

Error:66SyntaxError: Unrecognized token '\' 
+0

你如果你正在使用單詞邊界,還應該看到:http://stackoverflow.com/questions/2966535/javascript-regexp-and-boundaries。 – 2012-03-21 02:28:34

回答

5

當調用new RegExp()時,您必須將該模式作爲字符串傳遞。用引號括起來。

var regexp = new RegExp('\b[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}\b'); 

您還可以創建它使用特殊/pattern/分隔的語法,其中是報價:

var regexp = /[A-Z0-9._%+-][email protected][A-Z0-9.-]+.[A-Z]{2,4}/; 
0

我認爲,正則表達式應該是:

/\b[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}\b/ 
相關問題