Capturing groups regex with global flag in JavaScript -
i have use case, need allow processing arbitrary array of strings, arbitrary regex, created either regex literal, or through new regexp()
constructor.
it works fine, until global g
flag used capturing groups.
i read few answers on so, , proposed solution use regex.exec(string) in while loop, e.g. how access matched groups in javascript regular expression?, javascript regular expressions , sub-matches
i talked on irc, , advised against implementation together:
but there's regexes segfault engine, unless you're using spidermonkey.
so here corner case, try pasting fiddle or plunker, or console, breaks:
var regexstring = '([^-]*)'; var flags = 'ig'; var regex = new regexp(regexstring, flags); var arr = ['some-property-image.png', 'another-prop-video.png', 'y-no-work.bmp']; var result = []; arr.foreach(function(item) { var match; var inter = []; while (match = regex.exec(item)) { inter.push(match[0]); } }); console.log(result);
i did tried on regex101.com https://regex101.com/r/xg0cl4/1 breaks if without quantifier, i.e. /([^-])/g
https://regex101.com/r/yt7sq2/1
my question: (correct|safe)
way process arbitrary regexes against arbitrary strings?
it's not working because when '-' reached, exec() fails match '-' character, match 0 characters (because of *), doesn't skip it, , gets stuck. if use -|([^-]*)
, skip '-' character. need check 'match.index' property see if you've reach end.
also, should adding match[1]
not match[0]
if intent save matched text.
this works:
var regexstring = '-|([^-]*)'; // or better yet: '([^-]+)' work var flags = 'ig'; var regex = new regexp(regexstring, flags); var arr = ['some-property-image.png', 'another-prop-video.png', 'y-no-work.bmp']; var result = []; arr.foreach(function(item) { var match; var inter = []; while (match = regex.exec(item)) { if (match.index >= item.length) break; else if (match[1] !== void 0) inter.push(match[1]); } }); console.log(result);
but why not use 'match()' instead?
var regexstring = '[^-]+'; var flags = 'gi'; var regex = new regexp(regexstring, flags); var arr = ['some-property-image.png', 'another-prop-video.png', 'y-no-work.bmp']; var result = []; arr.foreach(function(item) { var inter = item.match(regex); }); console.log(result);