Can you retrieve multiple regex matches in JavaScript?

No, this is not possible in JavaScript (and most other regex flavors except Perl 6 and .NET). Repeated capturing groups always store the last value that was matched. Only .NET and Perl allow you to access those matches individually (match.Groups(i).Captures in .NET, for example).

You need two passes, the first to find the strings, the second to iterate over the matches and scan those for their sub-values.

Or make the regex explicit:

/^([0-9]{1,2}:)?([0-9]{1,2}:)?([0-9]{1,2}:)?([0-9]{0,2})?$/

Leave a Comment