replace < String < JavaScript
The replace() of a String returns a new string with the specified string replaced. The pattern can be a string or a RegExp, and the replacement can be a string or a function called for each match. If the pattern is a string, only the first occurrence will be replaced. The original string is left unchanged.
const paragraph = "I think Ruth's dog is cuter than your dog.";
const paragraphReplace = paragraph.replace("Ruth", "Cindy");
console.log(paragraphReplace);
// "I think Cindy's dog is cuter than your dog."
// example use a regex instead of a string
const regex = /Dog/i;
const paragraphReplaceRegex = paragraph.replace(regex, 'ferret');
console.log(paragraphReplaceRegex);
// "I think Ruth's ferret is cuter than your dog!"
// example replacing all instances of word
const text = "Java is awesome. Java is fun."
const pattern = /Java/g; // g is global for regex
const textReplace = text.replace(pattern, "JavaScript");
console.log(textReplace);
// "JavaScript is awesome. JavaScript is fun."