. Advertisement .
..3..
. Advertisement .
..4..
There are many ways to remove substring from string in JavaScript, including writing your own functions. But this tutorial will introduce only the most convenient built-in option that can get the job done.
Remove Substring From String In JavaScript Using replace()
The easiest way to remove a substring is to use the replace() function. It was originally designed to make some replacements based on the pattern provided by the user.
Syntax:
replace(substr/regexp, newsubstr/replacer)
There are two ways of using the replace() function. You can parse a substring directly and wrap it in a regular expression.
Example:
let origStr = "This is MDN!";
let newStr = origStr.replace("MDN", "ITTutoria");
console.log("Original string:", origStr);
console.log("New string:", newStr);
Example:
let origStr = "This is MDN!";
let newStr = origStr.replace(/MDN/g, "ITTutoria");
console.log("Original string:", origStr);
console.log("New string:", newStr);
Both examples above illustrate how you can get the same result with the two methods. They replace “MDN” with “ITTutoria” and print the same thing into the console:
Original string: This is MDN!
New string: This is ITTutoria!
If you want to remove a substring from a JavaScript string, just parse it and an empty string as the first and second argument of a replace() function.
Example:
let origStr = "Hello Mr. and Mrs. Smith!";
let newStr = origStr.replace(" and Mrs.", "");
console.log("Original string:", origStr);
console.log("New string:", newStr);
Example:
let origStr = "Hello Mr. and Mrs. Smith!";
let newStr = origStr.replace(/ and Mrs./, "");
console.log("Original string:", origStr);
console.log("New string:", newStr);
The snippets above both remove the same substring from a string:
Original string: Hello Mr. and Mrs. Smith!
New string: Hello Mr. Smith!
Remember that replace() only processes the first occurrence unless you use a regular expression or replaceAll(). Consider 3 examples below to understand this point.
Example 1:
let origStr = "John is a wonderful programmer, and he owns a wonderful house!";
let newStr = origStr.replace("wonderful ", "");
console.log("Original string:", origStr);
console.log("New string:", newStr);
Output:
Original string: John is a wonderful programmer, and he owns a wonderful house!
New string: John is a programmer, and he owns a wonderful house!
Example 2:
let origStr = "John is a wonderful programmer, and he owns a wonderful house!";
let newStr = origStr.replace(/wonderful /g, "");
console.log("Original string:", origStr);
console.log("New string:", newStr);
Output:
Original string: John is a wonderful programmer, and he owns a wonderful house!
New string: John is a programmer, and he owns a house!
Example 3:
let origStr = "John is a wonderful programmer, and he owns a wonderful house!";
let newStr = origStr.replaceAll("wonderful ", "");
console.log("Original string:", origStr);
console.log("New string:", newStr);
Output 3:
Original string: John is a wonderful programmer, and he owns a wonderful house!
New string: John is a programmer, and he owns a house!
As you can see, the first fails to replace the second occurrence, while replaceAll() and replace() with a regular expression both get the job done. The replaceAll() function works in the same way as replace(). The only difference is that it processes all matches of your pattern, not just the first.
Conclusion
To remove substring from string in JavaScript, you can use replace() and its sister function, replaceAll(). You can choose to provide the match in either a string or a regular expression, each with its own advantages. Bonus: if you want to do this in Python, check out our guide.
Leave a comment