how to replace characters in a string javascript

JavaScript
str.replace(/hello/g, 'hi');
// the g is to show it's a global change, not one-time change.

str.replace(new RegExp('hello', 'g'), 'hi');const search = 'duck'
const replaceWith = 'goose';

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

result; // => 'goose goose go'
Source

Also in JavaScript: