views:

56

answers:

1

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>
+6  A: 

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.

Tim Pietzcker
+1 I was just writing that, but you beat me to it!
Mark Byers
Thanks - very clever
mplungjan