Q:

How to Remove Text from a String in JavaScript

0

How to Remove Text from a String in JavaScript

All Answers

need an explanation for this answer? contact us directly to get an explanation for this 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-123ID-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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

JavaScript / jQuery Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to Check If an Array Exists and Not Empty in J... >>
<< How to Copy Array by Value in JavaScript...