开发者

how to extract string part and ignore number in jquery?

I have a string like foobar1,开发者_如何学C 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,"");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜