• 1
name Punditsdkoslkdosdkoskdo

Javascript how to remove text from a string

I've got a string that has data-123 as its value. How in jQuery or Javascript would I go in and remove the data- from the string while leaving the 123?

No jQuery needed.

var ret = "data-123".replace('data-','');
console.log(ret);   //prints: 123


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.

  • 0
Reply Report