replace array string javascript code example
Example 1: replace all occurrences of a string in javascript
const p = 'dog dog cat rat';
const regex = /dog/gi;
console.log(p.replace(regex, 'cow'));
Example 2: how to use the replace method in javascript
let string = 'soandso, my name is soandso';
let replaced = string.replace(/soandso/gi, 'Dylan');
console.log(replaced);
Example 3: javascript regex replace
const search = 'duck'
const replaceWith = 'goose';
const result = 'duck duck go'.replaceAll(search, replaceWith);
result;
Example 4: str replace javascript all
str.replace(/abc/g, '');