String.prototype.replace()
Replace a portion of text in a string with something else. The String.replace()
method accepts two arguments: the string to find, and the string to replace it with.
let text = 'I love Cape Cod potato chips!';
// returns "I love Lays potato chips!"
text.replace('Cape Cod', 'Lays');
By default, the String.replace()
method replaces the first match. To replace all matches, you’ll need to pass in a regular expression with the global flag (g
).
let chips = 'Cape Cod potato chips are my favorite brand of chips.';
// Only replaces the first instance of the word "chips"
chips.replace('chips', 'deep fried potato slices');
// Replaces all instances of the word "chips"
chips.replace(new RegExp('chips', 'g'), 'deep fried potato slices');
⏰ Preorder my new course on Web Components! Want to learn how to build Web Components from scratch, master best practices, and more? Preorder today and get $100 off of the launch price.
Find this useful? You can support my work by purchasing an annual membership.