如果我想用javascript匹配一些东西我可以使用foo.match(); 但我如何检查它是否不匹配...
为了更加明确,我倾向于使用!和.test(),例如:
var hasNoMatch = !/myregex/.test(string);
Run Code Online (Sandbox Code Playgroud)
因为规则.match()在没有匹配的情况下返回null,所以这也适用:
var hasNoMatch = !foo.match();
Run Code Online (Sandbox Code Playgroud)
从MDC文档中获取.match()(大多数时候资源更快):
如果正则表达式包含g标志,则该方法返回
Array包含所有匹配项的内容.如果没有匹配项,则返回该方法null.