Q:

how to use the replace method in javascript

var res = str.replace("find", "replace");
25

var str = "Mr Blue has a blue house and a blue car";

var res = str.replace(/blue/g, "red");
 
12
var str = "Visit Microsoft!";

var res = str.replace("Microsoft", "W3Schools");

console.log(res);
/*Result
Visit W3Schools!
*/
7
let string = 'soandso, my name is soandso';

let replaced = string.replace(/soandso/gi, 'Dylan');

console.log(replaced); //Dylan, my name is Dylan
9

New to Communities?

Join the community