Script Valley
Regex: Actually Useful Patterns
Groups and CapturingLesson 2.5

How to extract all matches with regex in JavaScript and Python

matchAll, findall, iterator protocol, destructuring group results, global flag requirement, match vs matchAll

Extracting All Matches and Their Groups

Extract all matches

match() with the g flag returns an array of full matches but drops group data. matchAll() returns an iterator of full match objects — each with group info intact.

const text = 'born 1990-01-15, died 2024-06-01';
const re = /(\d{4})-(\d{2})-(\d{2})/g;

for (const m of text.matchAll(re)) {
  console.log(m[1], m[2], m[3]);
}

matchAll() requires the g flag — calling it without g throws a TypeError.