2010-04-03 107 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