string replace javascript with regex code example
Example 1: replace regex javascript
const p = 'lazy dog. If the dog reacted, was it really lazy?';
const regex = /Dog/ig;
p.replace(regex, 'ferret')
// output: lazy ferret. If the ferret reacted, was it really lazy?
Example 2: javascript regex replace
const search = 'duck'
const replaceWith = 'goose';
const result = 'duck duck go'.replaceAll(search, replaceWith);
result; // => 'goose goose go'
Example 3: replace all javascript
str.split(search).join(replacement);