1
我有一個很大的javascript數組,5000條左右的條目。爲了以合理的表現方式對所有數組項進行RegExp匹配,我寧願不循環exec並拔出組。我發現沒有將exec分組的速度更快*。javascript正則表達式,匹配沒有組或正向逆序
由於JavaScript有沒有積極的回顧後操作,是否有可能採取以下值和匹配只有數字,而不使用標準正則表達式工具打誤報?
// the value to be matched
var reference_field = ',1,3,8,123,';
// a series of reference id to match
var re = /(?:3|8)(?=,)/g;
reference_field.match(re);
// result, note that the second three was not intended --> ["3", "8", "3"]
如果數組沒有那麼長,我只是對數字進行分組,
// the value to be matched
var reference_field = ',1,3,8,123,';
// a series of reference id to match
var re = /,(3|8)(?=,)/g;
var match;
while(match = re.exec(reference_field)){
if (match == null) {break;}
// do something with match[1]
}
......但就目前而言,由於手機是目標平臺,我對速度很敏感。我是否錯過了一個正則表達式的欺騙,還是不可能沒有分組。所有的javascript積極的lookbehind替代方法都不起作用(逗號上的負向前瞻),或引入額外的處理開銷。
確實!謝謝。 – pragmar 2012-02-08 04:32:24