How to Remove a Substring from a String in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
5 min

banner

# Table of Contents

  1. Remove a Substring from a String in JavaScript
  2. Remove all occurrences of a Substring from a String in JavaScript
  3. Remove a substring from a string using a regular expression
  4. Remove a substring from a string using String.slice()
  5. Remove all occurrences of a Substring from a String using str.split()

# Remove a Substring from a String in JavaScript

Call the replace() method with the substring and an empty string to remove a substring from a string.

The replace() method will return a new string where the first occurrence of the given substring is removed.

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

remove substring from string

The code for this article is available on GitHub

The String.replace() method returns a new string with one, some, or all matches of a regular expression replaced with the provided replacement.

The method takes the following parameters:

NameDescription
patternThe pattern to look for in the string. Can be a string or a regular expression.
replacementA string used to replace the substring match by the supplied pattern.

We want to remove the substring, so we used an empty string as the replacement.

index.js
const str = 'one,one,two'; // โœ… Remove first occurrence of substring from string const removeFirst = str.replace('one', ''); console.log(removeFirst); // ๐Ÿ‘‰๏ธ ",one,two"

The String.replace() method returns a new string with the matches of the pattern replaced. The method doesn't change the original string.

Strings are immutable in JavaScript.

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

# Remove all occurrences of a Substring from a String in JavaScript

Call the replaceAll() method with the substring and an empty string as parameters to remove all occurrences of a substring from a string.

The replaceAll method will return a new string where all occurrences of the substring are removed.

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

remove all occurrences of substring from string

The code for this article is available on GitHub

The String.replaceAll() method returns a new string with all matches of a pattern replaced by the provided replacement.

The method takes the following parameters:

NameDescription
patternThe pattern to look for in the string. Can be a string or a regular expression.
replacementA string used to replace the substring match by the supplied pattern.

The code sample removes all occurrences of the substring from the string by replacing each occurrence with an empty string.

# Remove a substring from a string using a regular expression

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, pass a regular expression as the first parameter to the replace method.

index.js
const str = 'one,one,two'; // โœ… 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"

remove substring from string using regular expression

The code for this article is available on GitHub

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

Inside 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 used the g (global) flag to match all occurrences of digits in the string.

If you ever need help reading a regular expression, check out this regular expression cheat sheet by MDN.

It contains a table with the name and the meaning of each special character with examples.

# Remove a substring from a string using String.slice()

You can also use the String.slice() method to remove a substring from a string.

index.js
const str = 'one,two,three'; const newStr = str.slice(0, 3) + str.slice(7); console.log(newStr); // ๐Ÿ‘‰๏ธ one,three

remove substring from string using slice

The code for this article is available on GitHub

The String.slice() method extracts a section of a string and returns it, without modifying the original string.

The String.slice() method takes the following arguments:

NameDescription
start indexThe index of the first character to include in the returned substring
end indexThe index of the first character to exclude from the returned substring

When only a single argument is passed to the String.slice() method, the slice goes to the end of the string.

The String.slice() method can be passed negative indexes to count backward.

index.js
const str = 'bobbyhadz.com'; console.log(str.slice(-3)); // ๐Ÿ‘‰๏ธ com console.log(str.slice(-3, -1)); // ๐Ÿ‘‰๏ธ co console.log(str.slice(0, -1)); // ๐Ÿ‘‰๏ธ bobbyhadz.co

If you need to get the index of a substring in a string, use the String.indexOf() method.

index.js
const str = 'one,two,three'; const substring = ',two'; const index = str.indexOf(substring); console.log(index); // ๐Ÿ‘‰๏ธ 3 const newString = str.slice(0, index) + str.slice(index + substring.length); console.log(newString); // ๐Ÿ‘‰๏ธ one,three

The String.indexOf() method returns the index of the first occurrence of a substring in a string.

If the substring is not contained in the string, the method returns -1.

You can use the String.lastIndexOf() method if you need to get the last index of a substring in a string.

index.js
const str = 'one,two,three,two'; const substring = ',two'; const lastIndex = str.lastIndexOf(substring); console.log(lastIndex); // ๐Ÿ‘‰๏ธ 13

# Remove all occurrences of a Substring from a String using str.split()

This is a two-step process:

  1. Use the String.split() method to split the string on the substring.
  2. Use the Array.join() method to join the array without a separator.
index.js
const str = 'one,one,two'; const result = str.split('one').join(''); console.log(result); // ๐Ÿ‘‰๏ธ ",,two"

remove all occurrences of substring from string using split

The code for this article is available on GitHub

The String.split() method takes a separator and splits the string into an array on each occurrence of the provided delimiter.

The String.split() method takes the following 2 parameters:

NameDescription
separatorThe pattern describing where each split should occur.
limitAn integer used to specify a limit on the number of substrings to be included in the array.
index.js
const str = 'one,one,two'; // ๐Ÿ‘‡๏ธ [ 'one', 'one', 'two' ] console.log(str.split(','));

The Array.join() method concatenates all of the elements in an array using a separator.

The only argument the Array.join() method takes is a separator - the string used to separate the elements of the array.

If the separator argument is set to an empty string, the array elements are joined without any characters in between them.

index.js
const str = 'one,one,two'; const result = str.split('one').join(''); console.log(result); // ๐Ÿ‘‰๏ธ ",,two"
The code for this article is available on GitHub

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev