How to replace all occurrences of a string in JavaScript
By Flavio Copes
Learn how to replace all occurrences of a string in JavaScript, with replaceAll, a global regex with the g flag, or by chaining split and join.
To replace all occurrences of a string in JavaScript you have 3 options: call replace() with a regular expression that has the g flag, chain split() and join(), or use the newer replaceAll() method.
Let’s see all three.
Using a regular expression
This simple regex will do the task:
String.replace(/<TERM>/g, '')
This performs a case sensitive substitution.
(You can test your pattern against sample text in my regex tester before using it.)
Here is an example, where I substitute all occurrences of the word ‘dog’ in the string phrase:
const phrase = 'I love my dog! Dogs are great'
const stripped = phrase.replace(/dog/g, '')
stripped //"I love my ! Dogs are great"
Be careful with the g flag. Without it, replace() only replaces the first occurrence and silently leaves the rest untouched. That’s the classic bug that brings people to this page.
To perform a case insensitive replacement, use the i option in the regex:
String.replace(/<TERM>/gi, '')
Example:
const phrase = 'I love my dog! Dogs are great'
const stripped = phrase.replace(/dog/gi, '')
stripped //"I love my ! s are great"
Notice that this time ‘Dogs’ was also matched, leaving just the ‘s’.
Remember that if the string contains some special characters, it won’t play well with regular expressions. A search term like '1+1' would be interpreted as a pattern, where + has a special meaning. So the suggestion is to escape the string using this function (taken from MDN):
const escapeRegExp = (string) => {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}
Using split and join
An alternative solution, albeit slower than the regex, is using two JavaScript functions.
The first is split(), which truncates a string when it finds a pattern (case sensitive), and returns an array with the tokens:
const phrase = 'I love my dog! Dogs are great'
const tokens = phrase.split('dog')
tokens //["I love my ", "! Dogs are great"]
Then you join the tokens in a new string, this time without any separator:
const stripped = tokens.join('') //"I love my ! Dogs are great"
Wrapping up:
const phrase = 'I love my dog! Dogs are great'
const stripped = phrase.split('dog').join('')
Since we pass a plain string to split(), there’s no regex involved, and no escaping problem with special characters.
Using replaceAll()
JavaScript now has a dedicated method for this, replaceAll(), added in ES2021:
const phrase = 'I love my dog! Dogs are great'
const stripped = phrase.replaceAll('dog', '')
stripped //"I love my ! Dogs are great"
When you pass a plain string as the first argument, every occurrence is replaced. No regex, no g flag to forget, no escaping needed.
You can still pass a regex to replaceAll(), for example for a case insensitive replacement, but it must have the g flag. Passing a regex without it throws a TypeError.
This is what I use today when I don’t need a regex pattern.