mpl*_*jan 1 javascript regex lookbehind
以下正则表达式是错误的 - 有人可以帮助找到所有:-)前面没有"请忽略我"吗?我之前不需要这样的正则表达式.单词边界可能会混淆事物.
谢谢
<script type="text/javascript">
function regexTest(string, assert) {
document.write('<hr>')
document.write(string)
document.write("[")
document.write(string.match(/\b((?!please ignore me )\:\-\))\b/gi)!=null);
document.write("]?" + assert);
}
regexTest("1. this is the first string :-) ",true);
regexTest("2. :-)",true)
regexTest("3. another string:-)here",true);
regexTest("4. Ending a sentence with :-)",true);
regexTest("5. please ignore me :-)",false);
</script>
Run Code Online (Sandbox Code Playgroud)
如果你想匹配一个前面:-)没有"请忽略我"的东西,那么你需要一个负面的lookbehind 而不是lookahead .但是,JavaScript不支持lookbehind.(?<!...)(?!...)
所以你可以做的是匹配(\bplease ignore me\s*)?:-\)然后,如果它匹配,检查捕获组是否$1为空.