How to Remove Text from a String in JavaScript
Topic: JavaScript / jQueryPrev|Next
Answer: Use the replace()
Method
You can simply use the replace()
method to replace text or characters from a string in JavaScript. This method returns a new string without modifying the original string.
For instance, if you have strings like ID-123
, ID-124
, and so on and you want to remove the ID-
part from the string, you can do something as shown in the following example:
Example
Try this code »// Sample string
var str = "ID-123";
// Replacing text by empty string
var newStr = str.replace("ID-", "");
console.log(newStr); // Prints: 123
Related FAQ
Here are some more FAQ related to this topic: