Table of Contents
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.
1 2 3 4 5 6 7 |
var my_string = "This program removes abc word from a string."; console.log("Original String: " + my_string); var word = 'abc'; my_string = my_string.replace(word,""); console.log("After removal: " + my_string); |
1 2 3 4 |
Original String: This program removes abc word from a string. After removal: This program removes word from a 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.
1 2 3 4 5 6 7 |
var my_string = "This program removes abc abc words from a string."; console.log("Original String: " + my_string); my_string = my_string.replace(/abc/g,""); console.log("After removal: " + my_string); |
1 2 3 4 |
Original String: This program removes abc abc words from a string. After removal: This program removes words from a 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.
Further reading:
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.
1 2 3 4 5 6 7 8 9 10 11 |
var my_string = "This program removes abc word from a string."; console.log("Original String: " + my_string); var word = 'abc'; var substring1 = my_string.substr(0,my_string.indexOf(word)-1); var substring2 = my_string.substr(my_string.indexOf(word) + word.length); my_string = substring1 + substring2; console.log("After removal: " + my_string); |
1 2 3 4 |
Original String: This program removes abc word from a string. After removal: This program removes word from a string. |
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 thestart
(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 thestart
as0
and thelength
up to1
less than the starting index of the word we wanted to remove using theindexOf()
method. - For the
substring2
, we set thestart
with the sum of the starting index of the word and the length of that word using theindexOf()
andlength
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.