Javascript – How to remove text from a string

javascriptstring

I've got a data-123 string.

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

Best 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.