JavaScript

How to Remove a Word from a String in JavaScript

Characters written inside quotes can be represented with the help of the Strings. Strings are commonly used in JavaScript to store and manipulate text. JavaScript allows us to manipulate strings easily and change the value of the variables in a string. The elimination of words from a string variable is one such operation.

This study will guide you through the methods for deleting a word from a string.

How to Remove a Word from a String in JavaScript?

For removing a word from a string, you can use the given predefined JavaScript methods:

    • substr() method
    • replace() method

Let’s check out each of the mentioned methods individually.

Method 1: Remove a Word from a String Using substr() Method

The “substring()” method assists in removing a word from a string. It extracts the needed part of the string by passing the start and the end index of the substring. As a result, a substring between the start and the last index will be returned.

Syntax

Follow the below-given syntax for using the substring() method:

str.substr(startIndex, endIndex)

 
Example

In this example, first, we will create a variable that stores the following string:

var str = "Welcome to the LinuxHint."

 
Next, we will call the substr() method by passing the starting index as “0” and the ending index as “10”. It will extract the string from 0 to 10 indexes as a substring:

console.log(str.substr(0, 10));

 
Output


The above method just gets the specific part of the string. However, in case of deleting a particular word from a string, follow the next section!

Method 2: Remove a Word from a String Using replace() Method

You can use the “replace()” method for removing a word from a string. It is the most frequently simplest method. It accepts two parameters and gives a string with a specific replacement. You can also utilize this method to remove a single occurrence of any specific word in a string.

Syntax

Follow the below-given syntax for using the replace() method:

replace("searchValue", "replaceValue")

 
Here, the “searchValue” is the value in a string that will be replaced, and the “replaceValue” is used as a replacement.

Let’s move to the example to learn more about the given method.

Example 1: Removing the First Occurrence of a Word from a String

First, we will create a variable named “str” that stores a string:

var str = "Welcome to the LinuxHint."

 
Now, we will remove the word “the” from the string by invoking the replace() method:

console.log(str.replace("the", ""));

 
As you can see, the specified word is removed from the string:


Now, if you want to remove a word that occurs multiple times in the same string, what would you do? For this purpose, follow the next example.

Example 2: Removing All Occurrences of a Word from a String

In this example, we will remove the word with multiple occurrences in the string using replace() method with regex; as the regex checks the full string and replaces all the occurrences with the specified replacement value.

Here, we will create a variable that holds a string of words that appear more than once:

var str = "The programmers can write code that is computer-understandable while Good programmers write code that is understandable by humans."

 
Now, we will replace all the “write” word with the empty string:

console.log(str.replace(/write/g, ""));

 
Here, the output shows that all the occurrences of the word “write” is successfully removed from the string:


We offered the methods related to removing words from the string.

Conclusion

For removing a word from a string, you can use the predefined JavaScript methods, including the substring() method and the replace() method. The substring() method extracts a specific part of the selected string, whereas the replace() method is used for removing a single word from the string, and also you can use it to remove all the occurrences of the same word in the string by using regex. In this study, we guided how to remove words from a string in JavaScript with proper examples.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.