How do I prevent javascript from replacing sections of words that match a string I’m trying to replace?
Here’s an example of what It’s doing:
function replaceApple(){
var aString = "apple app";
var replacing = "app";
return aString.replace(new RegExp(replacing, 'gi'),"cool");
}
result:
coolle cool
How can I make it so that it doesn’t mess with whole words? So that the result is:
apple cool
is there a fix for this or does one have to build a function that will loop through each word?
Result
Play with it on jsFiddle