Extract part of string and ignore numbers
case:
data: foobar1, foobaz2, barbar23, nobar100
a string like this
The numeric part is required foobar, foobaz, barbar, nobar
and ignored.
var s = "foobar1";
s = s.replace(/\d/g, "");
alert(s);
结果:
"foobar"
( \d
Is the regex class for "digits". We are replacing them.) Note that if given, it will remove any digits anywhere in the string.
Get only numbers and remove strings
data: foobar1, foobaz2, barbar23, nobar100
a string like this
The numeric part is required 1, 2, 23, 100
and ignored.
var s = "foobar1";
s = s.replace(/[^0-9]/ig,"");
alert(s);
结果:
"1"
([^0-9]/ig is the regex class for "not a digit". We are replacing them.) Note that if given, it will remove any digits anywhere in the string.