Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Naming Groups and Looping With matchAll()

In this lesson, you ported the loop that reads the whole week’s git log in one pass. The Python script compiles COMMIT with three named groups and re.MULTILINE, and loops over COMMIT.finditer(LOG), reading each part as commit["sha"].

  • Copied as it stands, the pattern stops the port before any line runs: SyntaxError: Invalid regular expression: … Invalid group. JavaScript checks a regex literal when the file loads; node --check reports the same error.

  • JavaScript spells a named group (?<sha>…), without Python’s P, which marks one of Python’s own extensions. Python’s re rejects (?<sha>…) in turn (re.PatternError: unknown extension ?<s at position 2), so a pattern copied either way needs this edit.

  • re.MULTILINE is the m flag, and it works the same way: ^ and $ mark each line. Without it, the pattern finds no commit, in either language.

  • With the g flag, LOG.match(COMMIT) returns the seven whole lines and no groups, where Python’s COMMIT.findall(LOG) gives each commit’s groups as a tuple. A loop over every match gets them back.

  • re.finditer() becomes text.matchAll(pattern), a method on the string, looped over with for...of: one full match per turn, groups included.

  • Each match keeps its names on its groups object: for the first commit, commit.groups.sha is "0d9a6c3" and commit.groups.date is "2026-11-06". Watch out: commit["sha"] is undefined, with no error, because the names live on groups.

  • matchAll() insists on the g flag, JavaScript’s “every match”: without g, it throws TypeError: String.prototype.matchAll called with a non-global RegExp argument.

  • Name your groups and read them from groups: as in Python, the names document the pattern.

  • Loop over matchAll() for every match with its groups, and keep match() with g for a plain list of the matched texts.

Here’s the loop, with the week’s log:

Language: JavaScript
const LOG = `
0d9a6c3 2026-11-06 PAY-2330: Payout  report  totals off by a cent
e8b2f13 2026-11-05 Revert "PAY-2288: Cache exchange rates"
7e3f5b8 2026-11-05 Bump stripe from 14.2.0 to 14.3.0
c2d4a61 2026-11-04 PAY-2317: Add the SEPA mandate to receipts
51f7e90 2026-11-03 Merge branch 'fix/pay-2304-retry-payments'
3ab08d2 2026-11-03 pay-2304: retry failed card payments.
9c1e4b7 2026-11-02 PAY-2291: Show refunds in their own currency
`;

const COMMIT = /^(?<sha>\w+) (?<date>\S+) (?<subject>.+)$/gm;

for (const commit of LOG.matchAll(COMMIT)) {
  console.log(commit.groups.date, commit.groups.sha);
}

It prints the same seven lines as the Python script:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In the last lesson, match() read one subject at a time. The script reads the whole git log in one pass: seven commits, each with its short hash, its date and its subject.

00:11 In this lesson, you’ll port the loop that reads the log. The Python script compiles COMMIT with three named groups. re.MULTILINE makes the caret and the dollar sign mark each line.

00:26 finditer() hands the loop one match per commit, and the loop reads each part by name. It prints seven lines, date and hash.

00:37 In the JavaScript port, the log becomes a template literal, folded here. The pattern copies over into a regex literal, as it stands. re.MULTILINE becomes the m flag, and it works the same way: the caret and the dollar sign mark each line.

00:55 Without it, in either language, the pattern finds no commit at all. The g flag asks for every match. Before you write the loop, check the pattern.

01:06 With g, match() returns every matching text. What does the file print? Your turn: pause the video and predict.

01:21 It prints nothing. JavaScript stops with a syntax error: invalid group.

01:29 The error points at the pattern. JavaScript spells a named group with a question mark and the name in angle brackets, without Python’s P. The P marks one of Python’s own extensions.

01:43 A regex literal is checked when the file loads, so no line runs at all. node --check, which only reads the file, reports the same error. The two spellings don’t overlap.

01:57 Python rejects JavaScript’s spelling in turn, so a pattern copied either way needs this edit.

02:04 Drop the three P’s. Run it again, and match() returns the seven commits. But they’re whole lines. Python’s findall() would hand you each commit’s groups, but with g, match() gives only the matched texts, so the names are gone.

02:21 A loop over every match gets them back.

02:26 For each commit’s parts, Python loops over the matches that finditer() hands it. In JavaScript, the string’s matchAll() method takes its place.

02:35 A for...of loop gets one full match per turn, groups included. Each match keeps its names on its groups property. For the first commit, the sha group holds the hash, and the date group holds the date.

02:50 Watch out for Python’s square brackets around a name. On a JavaScript match, they give undefined, with no error, because the names live on the groups object.

03:01 Run it. The seven lines match Python’s. And keep the g flag. In JavaScript, “every match” is the regex’s g flag, so matchAll() insists on it, and throws a type error without it.

03:18 Name your groups the JavaScript way, without the P, and read them from the match’s groups object. As in Python, the names document the pattern.

03:29 For every match with its groups, loop over matchAll() with the g flag. Keep match() with g for a plain list of the matched texts.

03:38 Backreferences and lookarounds are a later topic.

03:43 Next up is the exercise Read the Ticket Headers. There, three ticket headers are pasted into the time sheet. You’ll print each ticket’s number, team and title, and, for a header that names other tickets, only their numbers, the same lines the Python prints.

Become a Member to join the conversation.