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