How To Remove A Substring From A String Using JavaScript

Remove a Substring from a String using JavaScript

Hello guys. Today we will explain to you how to remove a substring from a string using JavaScript. We already tested effectively with three solutions: using the split() and join() method, using the replace(), and the substring() method. Hopefully, through this article, you can easily find an easy way to do that.

Remove A Substring From A String Using JavaScript

Using the split() and join() method

JavaScript provides a split() method to split a string into an array. The join() method joins all elements of the array as a string, and the filter() method fills out the element you want to be removed from the string.

Code example:

To remove a substring from a string, we can write the code this way.

  • First, we will call the split(' ') method to split the string on ‘ ‘.
  • Then, we filter the specified value by calling the filter() method and removing it.
  • Finally, rejoin all elements with ‘ ‘ value. And using the removed variable to get the result.

Let’s see the code example below.

const message = "The_password you entered is incorrect";
console.log("This is my String: ", message);
const substring = "The_password";

// Split the string and filter out "substring" and then rejoin with space character
const removed = message.split(" ").filter((substr) => substr !== substring).join(" ");
console.log("After removing a substring from a string: ", removed);

Output:

This is my String: The_password you entered is incorrect
After removing a substring from a string: you entered is incorrect

Using the substring() method

In this method, we use the JavaScript substring() to extract the specific substring from a string. This function is a built-in function in JavaScript, and it will return a portion of the string, start at the specified index, and extend for a given number of characters.

Example:

const message = "The_password you entered is incorrect";
console.log("This is my String: ", message);

// Calling the substring with 13 to remove the first 13 characters in the string
const removed = message.substring(13);
console.log("After removing a substring from a string: ", removed);

Output:

This is my String: The_password you entered is incorrect
After removing a substring from a string: you entered is incorrect

Using the replace() method

The last way we introduce in this post is using the replace() method, and we can replace a substring with another string or spaces as well. This method will replace the substring with a space character, and that is how the substring is removed from the string. Following the example below.

Example:

const message = "The_password you entered is incorrect";
console.log("This is my String: ", message);
const substring = "The_password";

// Using the replace method to replace the substring with ' '
const removed = message.replace(substring, "");
console.log("After removing a substring from a string: ", removed);

Output:

This is my String: The_password you entered is incorrect
After removing a substring from a string: you entered is incorrect

Summary

As you can see, we have many ways to remove a substring from a string using JavaScript, but we think the fastest and easiest way is using the replace() method. And what do you think? Leave your comment here if you have any questions about this article.

Thank you for your reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *