开发者

How do I grab the last value in a string

I know I can parse out the int by doing

parseInt($('#count_of_stations').html())

if the value is like this "2stations" that will return 2 but now I need to get the i开发者_JAVA技巧ntegers out in this format

one1
two2
three3
four4
five5

How do i get just the number out


If the number will always be 0-9, you can use slice() with a negative offset:

var num = $('#count_of_stations').text().slice(-1);

Alternatively, if the start of the string upto the number will always be the same, you can use slice() with a positive offset:

"mystring1".slice(8); // -> 1
"mystring12".slice(8); // -> 12

If those first two suggestions don't meet your requirements, you can use match() with a regular expression:

var num = $('#count_of_stations').text().match(/\d+$/)[0];


How about using a regular expression like \d$

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜