js string regex replace code example

Example 1: js replace characters in a string

var str = "Original string!";
var str = str.replace("Original", "New");

Example 2: 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 3: javascript regex replace

const search = 'duck'
const replaceWith = 'goose';

const result = 'duck duck go'.replaceAll(search, replaceWith);

result; // => 'goose goose go'