I am looking to utilize regular expressions in wrapping HTML tags around specific words within a text,
Below is an example using JavaScript:
In the given scenario, the first "We" is not being replaced. Why is this happening and how can it be modified?
var str="Welcome Microsoft We are Microsoft! we wehas weo in the WE world we.";
var res = str.replace(/([\s\!\.])(micro|microsoft|we)([\s\!\.])/gi, "$1<em>$2</em>$3");
console.log(res);
// wrong:Welcome <em>Microsoft</em> We are <em>Microsoft</em>! <em>we</em> wehas weo in the <em>WE</em> world <em>we</em>.
// right:Welcome <em>Microsoft</em> <em>We</em> are <em>Microsoft</em>! <em>we</em> wehas weo in the <em>WE</em> world <em>we</em>.