Remove a Substring from a String using JavaScript

avatar

Borislav Hadzhiev

Tue Nov 16 20212 min read

banner

Photo by Zhouxing Lu

Remove a Substring from a String #

To remove a substring from a string, call the replace() method, passing it the substring and an empty string as parameters, e.g. str.replace("example", ""). The replace() method will return a new string, where the first occurrence of the supplied substring is removed.

index.js
const str = 'one,one,two'; // โœ… Remove first occurrence const removeFirst = str.replace('one', ''); console.log(removeFirst); // ๐Ÿ‘‰๏ธ ",one,two" // โœ… Remove all occurrences const removeAll = str.replaceAll('one', ''); console.log(removeAll); // ๐Ÿ‘‰๏ธ ",,two" // โœ… Remove first occurrence using regex const removeRegex = str.replace(/one/, ''); console.log(removeRegex); // ๐Ÿ‘‰๏ธ ",one,two" // โœ… Remove all occurrences using regex const removeRegexAll = str.replace(/one/g, ''); console.log(removeRegexAll); // ๐Ÿ‘‰๏ธ ",,two"

We passed the following 2 parameters to the String.replace method:

  1. a substring we want to match in the string
  2. the replacement for the first match

Because we want to remove the substring, we provided an empty string as the replacement.

The replace method does not change the original string, it returns a new string. Strings are immutable in JavaScript.

If you need to remove all occurrences of a substring from a string, use the String.replaceAll method.

To remove all occurrences of a substring from a string, call the replaceAll() method on the string, passing it the substring as the first parameter and an empty string as the second. The replaceAll method will return a new string, where all occurrences of the substring are removed.

index.js
const str = 'one,one,two'; // โœ… Remove all occurrences const removeAll = str.replaceAll('one', ''); console.log(removeAll); // ๐Ÿ‘‰๏ธ ",,two"

The replaceAll method takes the same 2 parameters the replace method takes - the search string and the replacement for each match.

Note that the replace and replaceAll methods can also be used with a regular expression.

If you don't have a specific substring that you need to remove, but rather a pattern that you have to match, you can pass a regular expression as the first parameter to the replace method.

index.js
// โœ… Remove first occurrence using regex const removeRegex = str.replace(/[0-9]/, ''); console.log(removeRegex); // ๐Ÿ‘‰๏ธ "23,one,two" // โœ… Remove all occurrences using regex const removeRegexAll = str.replace(/[0-9]/g, ''); console.log(removeRegexAll); // ๐Ÿ‘‰๏ธ ",one,two"

The forward slashes / / mark the beginning and end of the regular expression.

Inside of the regular expression we have a character class [] that matches all digits in the range of 0 - 9.

The first example only matches the first occurrence of a digit in the string and replaces it with an empty string.

In the second example, we use the g (global) flag to match all occurrences of digits in the string.

If you ever need help reading a regular expression, bookmark this regex cheatsheet from MDN. It's by far the best one out there.

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee