To remove a specific substring from a string in JavaScript, you can use the `replace` method of the String object.
In this example,
str
with a value of "Hello World"
.replace
method of the string object str
to replace the substring "World"
with an empty string ""
.replace
method returns a new string with the specified replacements.str
to the console.let str = "Hello World";
str = str.replace("World", "");
console.log("Modified string: " + str);
Modified string: Hello
In this example,
text
with a value of "I like apple pie"
.replace
method of the string object text
to replace the substring "apple"
with an empty string ""
.replace
method creates and returns a new string with the specified replacements.text
to the console.let text = "I like apple pie";
text = text.replace("apple", "");
console.log("Modified string: " + text);
Modified string: I like pie
In this tutorial, we learned How to Remove specific Substring in JavaScript language with well detailed examples.