2010-08-22 24 views
1

我知道我可以做如何在一個字符串

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

解析出INT如果值是這個樣子「2stations」,將返回2,但現在我需要得到搶到的最後一個值整數以這種格式

one1 
two2 
three3 
four4 
five5 

我如何得到的只是數出

回答

3

如果該號碼將始終是0-9,你可以使用slice()對於一個負失調:

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

另外,如果字符串高達數開始永遠是一樣的,你可以使用切片()與正偏移:

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

如果前兩個建議不符合您的要求,您可以使用match()用正則表達式:

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

如何使用正則表達式像\ d $