I have a JS function that replaces a (full) word in a string:
var replaceWord = function(str,src,tgt){
var regex = new RegExp("\\b"+src+"\\b","g");
return str.replace(regex, tgt);
}
However it does not work when surounded by non-alpabeticall characters, for example:
let txt= "The 'great' idea of today ";
let word = "'great'";
let out = replaceWord(txt,word,"|test|");
console.log(out);
The output remains unchanged from the original. The desired output should have been:
The |test| idea of today
How can I change the function in order to have an accurate replacement function?
Help appreciated