JavaScript

How to Remove Substring From String in JavaScript

JavaScript provides various built-in features to manipulate strings. The replace() method is the commonly utilized method to replace or remove substrings in JavaScript. The replace() method is used to match the substring and then remove the matched part. The regular expressions can also be used in the replace() method to make a perfect match of the substring.

In this post, we have provided numerous methods for removing a substring from an existing string in JavaScript.

How to Remove Substring in JavaScript?

JavaScript provides a replace() method to remove substrings from strings. Moreover, users can replace a substring in an existing string as well. The method takes input as a substring to remove the string. For this purpose, the syntax is given below:

Syntax

str.replace(rem_substring, newSubstring);

The parameters are described as follows.

  • rem_substring: specifies the substring to be removed.
  • newSubstring: refers to the new string that replaces the existing one.

Example 1: Using the replace() Method to Remove the Substring in JavaScript

A built-in replace() method is utilized to remove the substring with an empty string in JavaScript. The method accepts the first argument as the substring for removal. While the second argument replaces the substring provided in the first argument. The following example utilizes the replace() method:

Code

console.log("An example to remove substring");
let str = 'What is JavaScript World';
let newStr = str.replace('JavaScript','');
console.log(newStr);

The description of the code is given below.

  • First, the string “What is JavaScript World” is stored in the “str” variable in JavaScript.
  • After that, the replace() method is employed to remove the “JavaScript” substring and replace it with the empty string “” (empty).
  • In the end, the console.log() method is utilized to display the updated string in the console window.

Output

The output shows that the “JavaScript” string has been removed from the existing string “What is JavaScript World”.

Example 2: Using Regular Expression to Remove Substring in JavaScript

A regular expression is adapted with the replace() method for removing a substring from an existing string in JavaScript. In this method, two arguments are passed as input. The first argument represents the regular expression “/remove_string/g” that the user wants to remove. The second argument refers to the empty string which performs replacement with the original string. The following code is employed to remove the substring.

Code

console.log("Another example to remove substring");
let str = 'Welcome to JavaScript World';
let newStr = str.replace(/JavaScript/g, '');
console.log(newStr);

In this code:

  • A str variable is utilized to store the string “Welcome to JavaScript World”.
  • After that, the regular expression “/JavaScript/g” is used to remove the substring “JavaScript” from the existing string.
  • Finally, newStr is utilized to display the new string by removing the substring.

Output

The output validates that the “JavaScript” substring has been removed from the existing string “Welcome to JavaScript World” in the console window.

Conclusion

In JavaScript, the replace() method is utilized to remove the substring with an existing string. The replace() method accepts two arguments. The first one represents the substring to be matched, and the second argument will be an empty string. This empty string will be replaced with the matching substring to remove it from the string. The matching string can be carried out using regular expressions as well. Here, you have learned the removal of a substring from an existing string in JavaScript.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.