replace all the words starting with a in string javascript code example
Example 1: replacing each space in a string javascript
const name = 'Hi my name is Flavio'
name.replace(/\s/g, '')
Example 2: 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 3: how to replace all the string in javascript when the string is javascript variable
function name(str,replaceWhat,replaceTo){
var re = new RegExp(replaceWhat, 'g');
return str.replace(re,replaceTo);
}