How to extract string part in jquery and ignore numbers or get only numbers strip string

Extract part of string and ignore numbers 

case:

data: foobar1, foobaz2, barbar23, nobar100a string like this

The numeric part is required foobar, foobaz, barbar, nobarand ignored.

var s = "foobar1";
s = s.replace(/\d/g, "");
alert(s);
结果:
 "foobar"

( \dIs 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, nobar100a string like this

The numeric part is required 1, 2, 23, 100and 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.

Guess you like

Origin blog.csdn.net/qq_34861341/article/details/100025406