string replace function code example
Example 1: javascript replace
var res = str.replace("find", "replace");
Example 2: javascript regex replace
const search = 'duck'
const replaceWith = 'goose';
const result = 'duck duck go'.replaceAll(search, replaceWith);
result;
Example 3: javascript replace
const p = 'dog dog cat rat';
const regex = /dog/gi;
console.log(p.replace(regex, 'cow'));
console.log(p.replace('dog', 'cow'));
Example 4: .replace
const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
console.log(p.replace('dog', 'monkey'));
const regex = /Dog/i;
console.log(p.replace(regex, 'ferret'));