How to remove text from a string?
I've got a
How can I remove
data- from the string while leaving the
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.
Read more... Read less...
replace function for this:
var str = "data-123"; str = str.replace("data-", "");
You can also pass a regex to this function. In the following example, it would replace everything except numerics:
str = str.replace(/[^0-9\.]+/g, "");
You can use
"data-123".replace('data-','');, as mentioned, but as
replace() only replaces the FIRST instance of the matching text, if your string was something like
will only replace the first matching text. And your output will be
So if you want all matches of text to be replaced in string you have to use a regular expression with the
g flag like that:
And your output will be
you can use slice() it returens charcters between start to end (included end point)
string.slice(start , end);
here is some exmp to show how it works:
var mystr = ("data-123").slice(5); // jast define start point so output is "123" var mystr = ("data-123").slice(5,7); // define start and end so output is "12" var mystr=(",246").slice(1); // returens "246"
var value="Data-123"; var removeData=value.replace("Data-",""); alert(removeData);
Hopefully this will work for you.