javascript replace all string occurrences code example
Example 1: js replace all substrings
/// replace "abc" with "" (blank string)
str.replace(/abc/g, '');
Example 2: replace all occurrences of a string in javascript
const p = 'dog dog cat rat';
const regex = /dog/gi;
console.log(p.replace(regex, 'cow'));
//if pattern is regular expression all matches will be replaced
//output: "cow cow cat rat"
Example 3: replace each string by another string javascript
str.split(search).join(replacement)