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 --checkreports the same error. -
JavaScript spells a named group
(?<sha>…), without Python’sP, which marks one of Python’s own extensions. Python’srerejects(?<sha>…)in turn (re.PatternError: unknown extension ?<s at position 2), so a pattern copied either way needs this edit. -
re.MULTILINEis themflag, and it works the same way:^and$mark each line. Without it, the pattern finds no commit, in either language. -
With the
gflag,LOG.match(COMMIT)returns the seven whole lines and no groups, where Python’sCOMMIT.findall(LOG)gives each commit’s groups as a tuple. A loop over every match gets them back. -
re.finditer()becomestext.matchAll(pattern), a method on the string, looped over withfor...of: one full match per turn, groups included. -
Each match keeps its names on its
groupsobject: for the first commit,commit.groups.shais"0d9a6c3"andcommit.groups.dateis"2026-11-06". Watch out:commit["sha"]isundefined, with no error, because the names live ongroups. -
matchAll()insists on thegflag, JavaScript’s “every match”: withoutg, it throwsTypeError: 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 keepmatch()withgfor a plain list of the matched texts.
Here’s the loop, with the week’s log:
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: