I have an large javascript array, 5000 or so entries. In order to run a RegExp match over all array items in a reasonably performant manner, I’d prefer not to loop exec and pull out groups. I’ve found the matching without grouping off exec to be much faster*.
- http://jsperf.com/regexp-exec-vs-match-vs-test/4 (speed test)
Given javascript has no positive lookbehind operation, is it possible to take the following value and match only the numbers without hitting false positives using the standard regex toolkit?
// 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"]
If the array wasn’t so long, I’d just group the number, e.g.
// 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]
}
… but as it stands, I’m sensitive to speed as mobile is a target platform. Am I missing a piece of regex trickery, or is it not possible without grouping. All the javascript positive lookbehind alternatives either don’t work (negative lookahead on the comma), or introduce additional processing overhead.
The
\bsequence could work for you — it matches a word boundary, both the beginning and end of words. If you know your strings are always these comma-separated number lists then this would find appropriate matches:You also wouldn’t need the extra commas at the start and end of
reference_field.