string replace with regex if matches javascript code example
Example 1: string replace javascript
let re = /apples/gi;
let str = "Apples are round, and apples are juicy.";
let newstr = str.replace(re, "oranges");
console.log(newstr)
output:
"oranges are round, and oranges are juicy."
Example 2: js regex replace multiple matches
str.replace(/-|:|\./g,''); // replace all matches of "-", ":" and "." in str