How to remove text from a string?

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.

Leave a Comment