Matthew C.
—You want to replace all occurrences of a substring in a string. This could be a character, a word, or multiple words. For example, you want to kebab case a string of words:
const myString = "the quick brown fox";
You need to replace all the empty spaces
with a -
. How do you do this?
The simplest and best way to replace multiple occurrences of a substring in a string is to use the replaceAll
method. There are other methods you can use if you need to support older browsers.
replaceAll()
MethodThe replaceAll()
method has two arguments: pattern
and replacement
. It returns a new string with all matches of the pattern
replaced by a replacement
. The pattern
is a string or regular expression (RegExp
). The replacement
is a string or function called for each match.
To replace all the empty spaces
with a -
in the example string below, you can call replaceAll()
with the following two string arguments:
const myString = "the quick brown fox"; const result = myString.replaceAll(" ", "-"); console.log(result); // the-quick-brown-fox
Using a regular expression for the pattern
argument is useful for more complex replacements, such as case-insensitive replacements:
const myString = "Apples pears apples strawberries"; const pattern = /apples/gi; const replacement = "grapes"; const result = myString.replaceAll(pattern, replacement); console.log(result); // grapes pears grapes strawberries
If the pattern
argument uses a regular expression, it must be a global regular expression. This is indicated by the g
flag. If you don’t add the g
flag, you’ll get the following error:
Uncaught TypeError: replaceAll must be called with a global RegExp
The replaceAll()
method is relatively new. It was introduced in ES2021 and works with all major browsers. If you have to support older versions of browsers, check that they support replaceAll()
.
replace()
MethodThe replace()
method is similar to the replaceAll()
method, the difference being that if the pattern
argument is a string, only the first occurrence will be replaced. You can get the replace()
method to replace all occurrences by using a global regular expression for the pattern
argument:
const myString = "Apples pears apples strawberries"; const pattern = /apples/gi; const replacement = "grapes"; const result = myString.replace(pattern, replacement); console.log(result); // grapes pears grapes strawberries
split()
and join()
You can also replace all occurrences of a string by first passing in the substring to be replaced in the split()
method and then using the join()
method to join the returned array with the new substring.
The split()
method searches for the passed-in separator
argument in the string. The separator
is the pattern that describes where each split should occur — the substring to be replaced. The split()
method returns an array of strings, split at each point where the separator
occurs.
The join()
method returns a new string by concatenating all of the elements in the array returned by the split()
method. The array elements are joined using the new replacement substring.
const myString = "the quick brown fox"; const resultArr = myString.split(" "); console.log(resultArr); // [ "the", "quick", "brown", "fox" ] console.log(resultArr.join("-")); // the-quick-brown-fox
This method is not suitable for more complex replacements, such as case-insensitive replacements.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.