snippetjavascriptCritical
How to remove text from a string?
Viewed 0 times
howfromtextremovestring
Problem
I've got a
How can I remove
data-123 string.How can I remove
data- from the string while leaving the 123?Solution
var ret = "data-123".replace('data-','');
console.log(ret); //prints: 123
Docs.
For all occurrences to be discarded use:
var ret = "data-123".replace(/data-/g,'');PS: The replace function returns a new string and leaves the original string unchanged, so use the function return value after the replace() call.
Code Snippets
var ret = "data-123".replace(/data-/g,'');Context
Stack Overflow Q#10398931, score: 1919
Revisions (0)
No revisions yet.