I wanna make a function in javascript
function replaceAll(str, find, replaceBy)
that replaces all occurrences of a specified word in a string, if an occurrence is surrounded by whitespace (/s/ - not just " ".). In addition it is okay, if an occurrence is at the beginning of the string (but has whitespace on the rightside), or if the occurrence is the last word (but has whitespace on its leftside).
I've tried to do a while loop, but it got very ugly. I believe this is doable with some intelligent regex, though I'm not really skilled with regular expressions.
Sample input
replaceAll("I went down to the III Isupermarket. I never<I> go anywhere outside my \n house I", "I", "he");
Sample output
"he went down to the III Isupermarket. he never<I> go anywhere outside my \n house he"
Any help much apreciated! Thanks