For example:
var htmlString = "It's a <span title='mark'>nice day</span> and also a <span title=''>sunny day</span>, it's day for surfing.";
want to replace the last two words "day" with "night", and skip the first one with tag span title "mark".
var replaceString = "day";
var reg=new RegExp("(?!title=\'mark\'>).*"+replaceString+".*(?!<\/span>)","gi")
var bb=htmlString.replace(reg,"night");
alert(bb)
// I can not get the right result with the above code
// Final result wanted: "It's a <span title='mark'>nice day</span> and also a <span title=''>sunny night</span>, it's night for surfing.";
UPDATE: the following works, but only matches 3 "day" in a sentence, how to make it match uncertain numbers of "day"?
alert(htmlString.replace(/(<span.*?'(?!mark)'>.*?)day(.*?<\/span>)|(?!>)day/gi, "$1night$2"));
Thanks.