I have the following string:
"i like ??dogs? and cats"
I want to identify the substring ??dogs? and replace it for something else, let's say "?birds".
So I created this function to do it:
function strUnderline (str, oldword, newword) {
str = str.replace(new RegExp('(^|[-,.!?:;"\'\(\\s])' + oldword + '(?=$|[-?!,.:;"\'\)\\s])'), "$1" + newword)
return str;
};
But when I run:
strUnderline("i like ??dogs? and cats", "??dogs?", "?birds")
i get:
"i like ???birds? and cats"
I want to define word boundaries and also capture them.
Any suggestions?