Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
JS Regex: Access all matches of a capture group instead of just the last

My regex looks something like this:

(\d \d)(?:, (\d \d))*

With which I match pairs of numbers, like 1 2, 3 4, 5 6. Capture group 1 would match once here, 1 2, while capture group 2 would match twice, both 3 4 and 5 6, which I both need.

My problem is that

"1 2, 3 4, 5 6".match(/(\d \d)(?:, (\d \d))*/)

returns

["1 2, 3 4, 5 6", "1 2", "5 6"]

Whereas what I really need is something like

["1 2, 3 4, 5 6", "1 2", "3 4", "5 6"]

or

["1 2, 3 4, 5 6", ["1 2"], ["3 4", "5 6"]]

Note: Of course the actual regex and problem are a bit more complicated, just matching \d \d multiple times on the string isn't really feasible for me.

This answer on a similar question for Perl is right in that this would be a good use case for an actual grammar, which I, however, am not going to implement in JavaScript

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!