replace globally js code example
Example 1: replace globally in javascript
var str = str.replace(/ /g,'-');
Example 2: javascript replace
var res = str.replace("find", "replace");
Example 3: replace all occurrences of a string in javascript
const p = 'dog dog cat rat';
const regex = /dog/gi;
console.log(p.replace(regex, 'cow'));
Example 4: replace all javascript
str.split(search).join(replacement);
Example 5: 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'));