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:
// Sample string
var str = "ID-123";
// Replacing text by empty string
var newStr = str.replace("ID-", "");
console.log(newStr); // Prints: 123
Use the
replace()MethodYou 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
need an explanation for this answer? contact us directly to get an explanation for this answerID-123,ID-124, and so on and you want to remove theID-part from the string, you can do something as shown in the following example: