Hypothetically speaking... I need to replace "spotted" with "white" only if the word "spotted" doesn't come after "big" on the same line. I can see this being a no go with the lack of negative lookbehinds in JS.
(1)Is replaced
Toby is a spotted cat
(2)Isn't replaced
Toby is a big spotted cat
(3)Is replaced
Toby is a spotted fluffy cat and he is big
(4)Isn't replaced
Not all big cats like Toby are spotted
-----
So putting all that together, a global search replace should turn:
Toby is a spotted cat
Toby is a big spotted cat
Toby is a spotted fluffy cat and he is big
Not all big cats like Toby are spotted
Into:
Toby is a white cat
Toby is a big spotted cat
Toby is a white fluffy cat and he is big
Not all big cats like Toby are spotted
Something to the effect of (if only negative lookbehinds worked in JavaScript:
<script>
var re = /(\w*\b) spotted/gi
function ReplaceDemo(s)
{
s1 = s.replace(re,"e;$1 white"e;);
if(RegExp.$1!="e;big"e;) return "e;Original : "e; + s + "e;\r\nChanged : "e; + s1 + "e;\r\nMatch : "e; + RegExp.$1;
else return "e;Original : "e; + s + "e;\r\nNot Changed!"e;;
}
function RunIt()
{
sentences = Array("e;Toby is a spotted cat"e;,"e;Toby is a big spotted cat"e;,"e;Toby is a spotted fluffy cat and he is big"e;,"e;Not all big cats like Toby are spotted"e;)
for(i=0;i<4;i++)
alert(ReplaceDemo(sentencesїi]))
}
RunIt()
</script>
<script>
var re = /(\w*\b.*) spotted/gi
var bigre = /big/i;
function ReplaceDemo(s)
{
s1 = s.replace(re,"e;$1 white"e;);
match = RegExp.$1;
if(match.search(bigre)==-1) return "e;Original : "e; + s + "e;\r\nChanged : "e; + s1 + "e;\r\nMatch : "e; + RegExp.$1;
else return "e;Original : "e; + s + "e;\r\nNot Changed!"e;;
}
function RunIt()
{
sentences = Array("e;Toby is a spotted cat"e;,"e;Toby is a big spotted cat"e;,"e;Toby is a spotted fluffy cat and he is big"e;,"e;Not all big cats like Toby are spotted"e;)
for(i=0;i<4;i++)
alert(ReplaceDemo(sentencesїi]))
}
RunIt()
</script>
It looks like at last I am getting hang of it. lol
Just one regxep (you just use e negative lookahead on EVERY character up to the pattern that's disallowed and then STOP here).
I believe logically this will do the same as a negative lookbehind and since JS doesn't support lookbehinds this is a good workaround - may be a useful to remember
theString = "e;Toby is a spotted cat\r\n"e;;
theString += "e;Toby is a big spotted cat\r\n"e;;
theString += "e;Toby is a spotted fluffy cat and he is big\r\n"e;;
theString += "e;Not all big cats like Toby are spotted\r\n"e;;
newString = theString.replace(/^((?:.(?!big))*)spotted/gmi, "e;$1white"e;);
alert (newString);