I am using a RegEx to match different possibilities.
What I need is a way to know which possilbility (group) is the match.
I've created a pretty simple snippet just to illustrate what I'd like to do:
var str = "aabbcc";
var re = /(aa)|(cc)/gi;
while ((match = re.exec(str)) != null) {
console.log(match.index);
// console.log(match.groupindex); <-- I need that
// Doing things here for each match, so I can't really mess up all the RegEx
}
In case aa
is matched it should return 1
,
in case cc
is matched it should return 2
.
Is there any way to do that?
Thanks for any answer.
PS: As I am not a RegEx expert, any comment to enhance this piece of code would be welcomed as well.
EDIT:
Thanks for the “helpfull” link, but as I am using a function for each match, I can't just match everything like you have suggested by marking as duplicate.