开发者

String To integer JavaScript

I want to get the integer in this string xyza开发者_开发知识库bc123.


You can replace everything that is not a number with a regex...

var number = 'xyzabc123'.replace(/[^\d]+/g, '');

See it on jsFiddle.

Update

To protect yourself from a string like this: b0ds72 which will be interpreted as octal, use parseInt() (or Number(); Number is JavaScript's number type, like a float.)

number = parseInt(number, 10);


to add to alex's answer if you wanted to get a functional integer

var number = 'xyzabc123'.replace(/[^\d]+/, '');
number = parseInt(number,10);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜