2013-04-10 89 views
0

仍然在javascript空間之前得到最後的數字數據,這是低於如何使用正則表達式

var id = "10101-Building and Construction 21"; 

基本上,我有從其他計算ID的數據我的問題,我想分開,21

這裏的結果是21;

我如何刪除身份證的價值沒有21。

請幫忙嗎?

+0

你不能讓其他計算回報的東西不是一個字符串更有意義? – Bergi 2013-04-10 12:19:50

+1

您是否想要在空格之後獲取最後一個數字值(例如,上例中的「21」)?數據總是以最後一個數值感興趣的相同格式存在嗎? – 2013-04-10 12:20:08

+0

'id = id.replace('10101-Building and Construction','')':) – adeneo 2013-04-10 12:20:17

回答

1

也許結合子和lastindexOf,是這樣的:根據你的問題做出

var result = id.substring(id.lastIndexOf(' ')+1); 
+0

你的代碼不行,請另外一個建議?這裏id會動態變化,而不是固定長度,所以我需要從最後到仍然在空間之前。 – 2013-04-10 12:58:09

+1

此代碼提取位於最後一個空格(空格)之後的id部分,如果需要,我可以向您顯示一個小提琴的寬度,您能解釋它爲什麼不符合您的需要,以便最終修改它? – 2013-04-10 13:43:16

0
$(document).ready(function(){ 
var id = 10101-Building and Construction 21; 
var result=id.substring(id.Length-2,id.Length) 
}); 
+0

基本上,id數據會動態變化,長度不固定,所以我需要從最後到尚在空間之前。任何暗示 – 2013-04-10 12:54:16

+0

id會動態地變化而不是固定它的長度,所以我需要從最後到仍然在空間之前 – 2013-04-10 12:58:47

0

假設:

- 你從字符串extact瓦納的ID總是在最後
- 該ID始終是一個數字

  • 如果上述假設爲真:
  • ,那麼你可以寫一個JavaScript函數,並執行以下功能。從上

    1. 循環和檢查號碼,直到它,只要它encouters停止字符查找的字符串

    2. ,你會得到該電源線ID

javascript中的函數

function express() 
{ 
var str="10101-Building and Construction 21"; 
var temp,id; 
for(var t2=1;t2<=str.length;t2++) 
{ 
    temp = str.substring(str.length-t2); 

    var condition = isNaN(temp); 
    if(condition == true) 
    { 
     break; 
    } 
    id=temp; 
} 
document.write("<br>"+id); 
} 

這將提取ID

result:

ID = 12

Click Here : And the fiddle for a live example of this based on your values