This class gets all matches, with start and end position, within the string, for a given regexp.
From high level the source code is:
- Ensuring that each character is captured by a group: eg. /ab(cd)ef/ => /(ab)(cd)(ef)/
- Calling exec on the converted regexp with a given string
- Summing lengths of previous groups for start position of current group, add length of current group for end position
will setup parsed regexp, returns instance
will find all matching groups, returns array<{match: string, start: Number, end: Number}>
will find match for group number, returns {match: string, start: Number, end: Number}
let regex = /a(?: )bc(def(ghi)xyz)/g;
let regex2 = new MultiRegExp2(regex);
let matches = regex2.execForAllGroups('ababa bcdefghixyzXXXX');
console.log(matches);
// reset to beginning: regex2.regexp.lastIndex = 0;
Will output:
[ { match: 'defghixyz', start: 8, end: 17 },
{ match: 'ghi', start: 11, end: 14 } ]
If you want to include the full match (group 0) then add true as the second parameter.
Also available:
let matches = regex2.execForGroup('ababa bcdefghixyzXXXX', 2);
= { match: 'ghi', start: 11, end: 14 }
to compile the module to ES5 run npm install && npm run build && npm run test