Remove Word from String in JavaScript

Using replace() Method

We can use the replace() method to remove a word or words from a string in JavaScript.

Remove a Single Occurrence of a Word

To remove a single occurrence of a word from a string, use the replace() method to replace the word in the string with an empty string.

The replace() method in JavaScript is a string method that searches and replaces the occurrences of a given substring with another substring and returns the updated string. It holds arguments:

• The pattern represents a specific set of characters inside an existing string that needs replacing.

• The replaceValue states what should overwrite over the pattern.

We used the replace() method to overwrite an empty string over the pattern abc to remove the pattern.

Remove Multiple Occurrences of a Word

To remove multiple occurrences of a word from a string, use the replace() method with a RegExp to replace it with an empty string.

We have already discussed the replace() method while explaining the code snippet for removing a single word from the string using the replace() method. In this section, we used the method with a RegExp expression.

The RegExp expression RegExp is a sequence of characters that constructs a search pattern used to perform complex search and replace operations on strings in JavaScript. The patterns are built using the RegExp syntax. It can have several parameters enclosed in forward slashes /.

We provided the replace() method with /abc/g as a regular expression to search it into the string and replace it with an empty string to remove it. Here, the pattern was abc that we wanted to replace with an empty string, while the g flag is used for global search, which means it searched for all instances in String.

Using substr() Method

To remove word from string in JavaScript:

  • Use the substr() method to make two substrings by splitting the string into two parts, excluding the word from it.
  • Use + operator to concatenate the two substrings.

The JavaScript substr() method extracts a specified number of characters from the beginning or end of a string. It returns the part of the string between the start and count parameters but does not change the original string object. The arguments it holds are:

  • The start of the integer type denotes what position in a given String object we wish to begin extracting characters from.
  • An optional length of integer type represents how many characters should be extracted after the start (if no length argument is included, it will default to full end-string).

We used the substr() method to make substring1 and substring2 from the my_string:

  • For the substring1, we specified the start as 0 and the length up to 1 less than the starting index of the word we wanted to remove using the indexOf() method.
  • For the substring2, we set the start with the sum of the starting index of the word and the length of that word using the indexOf() and length functions, respectively.

After successfully creating two substrings, we used the + operator to concatenate the two substrings into my_string.

That’s all about how to remove word from String in JavaScript.

Was this post helpful?

Leave a Reply

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