Skip to content
JS
Recipe · Strings

How to Remove Substrings from Strings in JavaScript

Published 23 Jun 20233 methods
Using the replace() method
var originalString = "Hello World!";
var substringToRemove = " World";
var newString = originalString.replace(substringToRemove, "");

console.log(newString); // Outputs: "Hello!"

Other methods on this page: Using the slice() method, Using the substring() method

Using the replace() method

The JavaScript replace() method is used to replace a specified value or substring in a string with another value or substring. It creates a new string without altering the original one. To illustrate this, let’s consider an example where we want to remove the substring “World” from the string “Hello World”.


var str = "Hello World";
var newStr = str.replace("World", "");

How it works

The replace() method scans the original string for the first match of the substring specified to be replaced (in this case “World”). When the method finds the substring, it replaces it with the new value specified (in this case, an empty string “”).

  • The variable str is initialized with the string “Hello World”.
  • The replace() method is called on str with “World” as the value to be replaced and “” (empty string) as the replacement.
  • Since “World” exists in str, it is replaced with an empty string, effectively removing “World” from the string.
  • The new string is then stored in the variable newStr.
  • As a result, newStr will contain “Hello ” (with a trailing space) as it has removed “World”.

Using the slice() method

The JavaScript slice() method will be used to remove a substring from a string by identifying its start and end index. This technique will be demonstrated through an example.


var str = "Hello, World!";
var newStr = str.slice(0, 5) + str.slice(7);

How it works

The slice() method in JavaScript is used to extract a section of a string and returns it as a new string without modifying the original string. In this case, it is used to remove a substring by concatenating two sliced parts of the original string, effectively excluding the undesired substring.

  • The slice() method takes two parameters: the start index and the end index. The start index is inclusive, whereas the end index is exclusive.
  • In the provided code, two slices are made: one from index 0 to 5 (extracting “Hello”) and another from index 7 to the end of the string (extracting “World!”).
  • Notice that the substring from index 5 to 7, i.e., “, ” is left out. This effectively removes that substring from the original string.
  • The ‘+’ operator is then used to concatenate the two resulting strings, forming “HelloWorld!”
  • The original string remains unchanged, and we have a new string without the undesired substring.

Using the substring() method

The substring() method in JavaScript can be used to extract a part of a string, but it doesn’t remove substrings. To remove a substring, we can use the replace() method. The replace() method searches for a specific substring in a string and replaces it with another substring. Here’s how it works.


var originalString = "Hello World!";
var substringToRemove = " World";
var newString = originalString.replace(substringToRemove, "");

console.log(newString); // Outputs: "Hello!"

How it works

The JavaScript replace() method is used to replace a substring in a string with a new substring. It takes two parameters: the substring to find and the substring to replace it with. When the specified substring is found, it is replaced by the new substring and a new string is returned. The original string does not change.

  • Step 1: We define the original string: “Hello World!”.
  • Step 2: We define the substring we want to remove: ” World”.
  • Step 3: We use the replace() method on the original string. We pass the substring to remove as the first parameter and an empty string as the second parameter. This replaces the substring with nothing, effectively removing it.
  • Step 4: The replace() method returns a new string with the specified substring removed. We store this new string in a variable.
  • Step 5: We print the new string to the console. The output is: “Hello!”.