2014-02-15 50 views
0

嗨,我一直在试图检查是否有任何?或*存在于textarea的,但无法获得数正则表达式检查textarea中的几个特殊字符

我尝试

if($('textarea').val().match(/\**\?*/).length){ 
    //do something 
    console.log("* or ? not allowed"); 
} 

测试用例

some word ????? ***** 
some***** 
"*" "?" 
"?" 

“*”

它必须能够检查任何可能?或*出现在textarea。

+0

你的文字如果你想要所有的匹配,就需要一个全局标志 – elclanrs

+2

请遵循以下规则:'/ [*?]/g' – falsetru

回答

2

使用[*?]作为模式。它匹配*?。 (*?失去了特殊meaing内[...]和火柴字面上。)

使用RegExp.prototype.test,你不必指望比赛。

if (/[*?]/.test($('textarea').val()) { 
    // do something 
    console.log("* or ? not allowed"); 
} 

/[*?]/.test('hello world') // => false 
/[*?]/.test('hello * world') // => true 
/[*?]/.test('hello world?') // => true 
1

如果你想与match功能走,那么你可以做如下

if($('textarea').val().match(/[*?]/g).length > 0){ 
    //do something 
    console.log("* or ? not allowed"); 
} 
1

要检查是否存在包含问号或星号[\?*]{1,}