Question : Implementing regular expression in JavaScript

Basically, all I need to do is find all the matches in a sting that have the string “xxx” but not the string that have “yyy” before them.
So that:
“This is a sentence with XXX” will find a match and “This Is another sentence yyy with xxx” will also find a match, but
“this is a sentence with yyyxxx” will not find a match.

Answer : Implementing regular expression in JavaScript

Here you are.
Got an answer from Tim  Pietzcker


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.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
<script type="text/javascript">
var re = /(\bplease ignore me\s*)?:-\)/;

function regexTest(string, assert) {
	document.write('<hr>')
  document.write(string)
  document.write("[")
  var res = string.match(re)
  document.write(RegExp.$1=="")
 
  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>
Random Solutions  
 
programming4us programming4us