In this lesson, you ported the release-notes script’s first question about each commit: is this a ticket commit? The Python asks it with a compiled pattern, and counts the week’s seven subjects:
TICKET = re.compile(r"(PAY-\d+): (.+)", re.IGNORECASE)
It prints 4 ticket commits, 3 other commits.
-
JavaScript has no
remodule. A regular expression is a value of its own, already compiled: a regex literal, written between slashes:/(PAY-\d+): (.+)/i. It needs no quotes and norprefix, because what sits between the slashes isn’t a string, so a backslash is never a string escape. -
The flag goes after the closing slash:
iisre.IGNORECASE. -
The pattern language carries over on ASCII text:
\d,\wand\s, character classes, quantifiers, groups in parentheses and alternation read the same. -
A pattern answers yes or no through its
test()method. It returnstrueorfalse, with no match object. -
Ported line for line with
TICKET.test(subject), the count is5 ticket commits, 2 other commits, with no error. The extra one isRevert "PAY-2288: Cache exchange rates". -
Python’s
match()only tries at the start of the text, so it returnsNonefor the revert.test()searches anywhere, asre.search()does, and findsPAY-2288:at position 8. Python’ssearch()counts five too. -
JavaScript has one way to test, and it searches, so the place a match must start goes in the pattern:
^at the front means the start of the text, the anchor Python’smatch()adds for you. With/^(PAY-\d+): (.+)/i, the count is4 ticket commits, 3 other commits, and the full release notes match Python’s. -
One rule for Python’s three functions:
re.search()is the pattern as it is,re.match()adds^, andre.fullmatch()adds^and$, the end of the text. -
re.fullmatch(r"PAY-\d+", "PAY-2291x")isNone, but/PAY-\d+/.test("PAY-2291x")istrue, because it findsPAY-2291inside./^PAY-\d+$/.test("PAY-2291x")isfalse.isdigit()ports the same way:/^\d+$/.test("2291")istrue. -
includes()andstartsWith()don’t take a pattern. Pass one, and JavaScript stops withTypeError: First argument to String.prototype.includes must not be a regular expression. Test with a caret instead:test()with^does whatstartsWith()would. -
To check a value, anchor both ends, and try it on a string with extra characters on each side.
Here’s the count with the anchored pattern:
const TICKET = /^(PAY-\d+): (.+)/i;
const subjects = [
"PAY-2330: Payout report totals off by a cent",
'Revert "PAY-2288: Cache exchange rates"',
"Bump stripe from 14.2.0 to 14.3.0",
"PAY-2317: Add the SEPA mandate to receipts",
"Merge branch 'fix/pay-2304-retry-payments'",
"pay-2304: retry failed card payments.",
"PAY-2291: Show refunds in their own currency",
];
let tickets = 0;
let others = 0;
for (const subject of subjects) {
if (TICKET.test(subject)) {
tickets += 1;
} else {
others += 1;
}
}
console.log(
`${tickets} ticket commits, ${others} other commits`,
);
It counts what the Python counts:
4 ticket commits, 3 other commits
Next up is the text lesson Building Patterns With new RegExp(). There, you’ll build the ticket pattern from the team’s setting, so other teams can use their own key.