Reputation: 1
I want to replace every word including "before" except one particular word including "before" (case-insensitive)
Example: "before" = "after“; except "beforethree"
beforeone beforetwo beforethree beforefour
Result should be:
afterone aftertwo beforethree afterfour
My code is:
<script>
function Replace() {
var str = document.getElementById("id").innerHTML;
var res = str.replace(/before/gi, "after");
document.getElementById("id").innerHTML = res;
}
</script>
Thank you guys very much in advance!
Upvotes: 0
Views: 86
Reputation: 21672
Modify your replace to be this instead
<script>
function Replace() {
var str = document.getElementById("id").innerHTML;
var res = str.replace(/(?!beforethree)before/gi, "after");
document.getElementById("id").innerHTML = res;
}
</script>
Upvotes: 0
Reputation: 36511
You could use a negative lookahead regex instead:
/before(?!three)/gi
Upvotes: 4
Reputation: 6917
one option would be to
beforethree
with something safe like %%three
before
with after
%%
with before
I'm sure there are more elegant solutions out there
Upvotes: 0