var str = "JavaScript replace method test"; var res = str.replace("test", "success"); //res = Javscript replace method success
var myStr = 'this,is,a,test'; var newStr = myStr.replace(/,/g, '-'); console.log( newStr ); // "this-is-a-test"
let string = 'soandso, my name is soandso'; let replaced = string.replace(/soandso/gi, 'Dylan'); console.log(replaced); //Dylan, my name is Dylan
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');