嗨,我一直在尝试检查是否有?或 * 出现在文本区域但无法获取计数
我的尝试
if($('textarea').val().match(/\**\?*/).length){
//do something
console.log("* or ? not allowed");
}
测试用例
some word ????? *****
some*****
"*" "?"
"?"
“*”
它必须能够检查任何可能的?或 * 出现在文本区域中。
最佳答案
使用[*?]
作为模式。它匹配 *
或 ?
。 (*
、?
在 [...]
中失去其特殊含义并按字面匹配。)
使用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
关于javascript - 正则表达式检查文本区域中的一些特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21793704/