How to remove text from a string?

Michael Grigsby picture Michael Grigsby · May 1, 2012 · Viewed 1M times · Source

I've got a data-123 string.

How can I remove data- from the string while leaving the 123?

Answer

Mathletics picture Mathletics · May 1, 2012

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.