Tuesday, 15 September 2015

javascript - Get the index of the group that matched in a regexp? -



javascript - Get the index of the group that matched in a regexp? -

i have regexp:

/(alpha)|(beta)|(gamma)/gi

some text match against:

betamax. digamma. alphabet. hebetation.

the matches are:

beta, gamma, alpha, beta

the values looking be:

1,2,0,1

...can ascertain index of grouping matched in regexp?

to access groups, need utilize .exec() repeatedly:

var regex = /(alpha)|(beta)|(gamma)/gi, str = "betamax. digamma. alphabet. hebetation."; (var nums = [], match; match = regex.exec(str); ) nums.push(match.lastindexof(match[0]));

if want indizes zero-based, use

nums.push(match.slice(1).indexof(match[0]));

javascript regex indexing group match

No comments:

Post a Comment