replace a word in a string javascript code example
Example 1: javascript replace string
var str = "JavaScript replace method test";
var res = str.replace("test", "success");
Example 2: str replace javascript all
str.replace(/abc/g, '');
Example 3: replace all javascript
str.split(search).join(replacement);
Example 4: javascript string replace
const p = 'Its going to rain today and its going to rain tomorrow';
const regex = /rain/gi;
console.log(p.replace(regex, 'snow'));
console.log(p.replace('rain', 'snow'));