HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptCritical

How to remove text from a string?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howfromtextremovestring

Problem

I've got a 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.