2010-04-03 108 views
2

如何可以不允許這些字符:JavaScript的正則表達式

\ /「'[] {} |〜`^使用javascript正則表達式模式&

+0

的可能重複http://stackoverflow.com/questions/705672/regular-expression-to-allow-a-set-of-characters-and-disallow-others – outis 2010-04-03 09:36:52

+0

另外:http://stackoverflow.com/questions/756567/regular-expression-for-excluding-special-characters,http://stackoverflow.com/questions/1152844/regex-for-alphanumeric-and-the-character ,http://stackoverflow.com/questions/1763071/negate-characters-in-regular-expression,http://stackoverflow.com/questions/834002/regular-expression-that-includes-all-keyboard-characters-except - ,... – outis 2010-04-03 09:44:56

+0

(儘管PHP標籤):http://stackoverflow.com/questions/1778958/php-regular-expression-accept-selected-characters-only,http://stackoverflow.com/questions/ 878715 /檢查的字符串爲非法字符,使用-REG ular表達。 – outis 2010-04-03 09:46:12

回答

1

檢查一個字符串包含以下字符之一:

if(str.match(/[\\\/"'\[\]{}|~`^&]/)){ 
    alert('not valid'); 
} 

Validat EA整個字符串,開始到結束:

if(str.match(/^[^\\\/"'\[\]{}|~`^&]*$/)){ 
    alert('it is ok.'); 
} 
+2

或刪除這些字符:'str = str.replace(/ [\\\ /''\ [\] {} |〜\'^&]/g,「」);'' – Anonymous 2010-04-03 09:34:22