mplungjan
mplungjan

Reputation: 177684

Negative look-around

the following regexp is wrong - can someone please help to find all :-) that do not have "please ignore me " in front? I have not previously needed such an regexp. The word boundaries might confuse things.
Thanks

<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>

Upvotes: 1

Views: 1210

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336098

If you want to match a :-) that is not preceded by "please ignore me", then you'd need a negative lookbehind (?<!...) instead of lookahead (?!...). However, JavaScript doesn't support lookbehind.

So what you could do is to match (\bplease ignore me\s*)?:-\) and then, if it matches, check whether the capturing group $1 is empty.

Upvotes: 9

Related Questions