(js)正则表达式匹配字母排除短语

时间:2017-09-08 10:57:26

标签: javascript regex

我想匹配以感叹号开头的每个单词。 我到目前为止,它的工作原理。正则表达式:^!。 但现在我想排除“!test”这个词。所以!嗨匹配但是!测试没有。 没有NOT运算符,所以任何想法?

1 个答案:

答案 0 :(得分:0)

你可以使用正则表达式



function match(str){
    let regex = /^!(?!test)\w+/;
    return str.match(regex) != null;
}

console.log(match('!testing'))
console.log(match('!hihello'))




  

(?!test)是一个负向预测,确保!测试不匹配