2014-05-07 35 views

回答

3

由於沒有要提取的部分之前沒有數字,您可以使用下面

var num = String.match(/\d+/)[0]; 
+0

完美工作。非常感謝。 – Posz

0

match返回數組或null。您可以使用

string.match(/#\s*(\d+)\s*M/)[1] 

如果需要,添加一個測試:

var m = string.match(/#\s*(\d+)\s*M/); 
if (m) { 
    var num = +m[1]; // the first captured group is at index 1 
    console.log('number : ', num); 
} else { 
    console.log('no number') 
} 
+0

對於正則表達式的解釋:http://regex101.com/r/zQ2mM1 – Beterraba