1// Chrome 85+
2str.replaceAll(val, replace_val);
3// Older browsers
4str.replace(/val/g, replace_val);
1const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
2
3console.log(p.replaceAll('dog', 'monkey'));
4// expected output: "The quick brown fox jumps over the lazy monkey. If the monkey reacted, was it really lazy?"
1function name(str,replaceWhat,replaceTo){
2 replaceWhat = replaceWhat.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
3 var re = new RegExp(replaceWhat, 'g');
4 return str.replace(re,replaceTo);
5}
1//as of August 2020, not supported on older browsers
2str.replaceAll("abc","def")