javascript find and replace text in string code example
Example 1: replacing characters in string javascript
var myStr = 'this,is,a,test';
var newStr = myStr.replace(/,/g, '-');
console.log( newStr ); // "this-is-a-test"
Example 2: javascript replace
var res = str.replace("find", "replace");
Example 3: replace in string javascript
const p = 'hello world ! hello everyone ! ';
const regex = /hello/gi;
console.log(p.replace(regex, 'good morning'));
// expected output: "good morning world ! good morning everyone !"
console.log(p.replace('hello', 'good evening'));
// expected output: "good evening world ! hello everyone !"