I have a regexp:
/(alpha)|(beta)|(gamma)/gi
Some text to match against:
Betamax. Digamma. Alphabet. Hebetation.
The matches are:
beta, gamma, alpha, beta
The values I am looking would be:
1,2,0,1
...can I ascertain the index of the group that matched in the regexp?
If we consider the exact sample you provided, the below will work:
I leave it to you to make the content of the loop more dynamic / generic :p
Build your regex from an array of strings, and then lookup the matches with indexOf.
To access the groups, you will need to use
.exec()
repeatedly:If you want the indizes zero-based, you could use