简体   繁体   中英

how to extract string part and ignore number in jquery?

I have a string like foobar1, foobaz2, barbar23, nobar100 I want only foobar, foobaz, barbar, nobar and ignoring the number part.

If you want to strip out things that are digits, a regex can do that for you:

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

( \d is the regex class for "digit". We're replacing them with nothing.)

Note that as given, it will remove any digit anywhere in the string.

This can be done in JavaScript:

/^[^\d]+/.exec("foobar1")[0]

This will return all characters from the beginning of string until a number is found.

var str = 'foobar1, foobaz2, barbar23, nobar100';
console.log(str.replace(/\d/g, ''));

Find some more information about regular expressions in javascript...

This should do what you want:

var re = /[0-9]*/g;
var newvalue= oldvalue.replace(re,"");

This replaces al numbers in the entire string. If you only want to remove at the end then use this:

var re = /[0-9]*$/g;

I don't know how to do that in JQuery, but in JavaScript you can just use a regular expression string replace.

var yourString = "foobar1, foobaz2, barbar23, nobar100";    
var yourStringMinusDigits = yourString.replace(/\d/g,"");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM