This regular expression matches numbers at the end of the string.
var matches = str.match(/d+$/);
It will return an Array
with its 0
th element the match, if successful. Otherwise, it will return null
.
Before accessing the 0
member, ensure the match was made.
if (matches) {
number = matches[0];
}
jsFiddle.
If you must have it as a Number
, you can use a function to convert it, such as parseInt()
.
number = parseInt(number, 10);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…