Loading video player…

Testing Subjects With a Regex Literal

Testing Subjects With a Regex Literal

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:

Language: Python
TICKET = re.compile(r"(PAY-\d+): (.+)", re.IGNORECASE)

It prints 4 ticket commits, 3 other commits.

  • JavaScript has no re module. 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 no r prefix, 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: i is re.IGNORECASE.
  • The pattern language carries over on ASCII text: \d, \w and \s, character classes, quantifiers, groups in parentheses and alternation read the same.
  • A pattern answers yes or no through its test() method. It returns true or false, with no match object.
  • Ported line for line with TICKET.test(subject), the count is 5 ticket commits, 2 other commits, with no error. The extra one is Revert "PAY-2288: Cache exchange rates".
  • Python’s match() only tries at the start of the text, so it returns None for the revert. test() searches anywhere, as re.search() does, and finds PAY-2288: at position 8. Python’s search() 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’s match() adds for you. With /^(PAY-\d+): (.+)/i, the count is 4 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 ^, and re.fullmatch() adds ^ and $, the end of the text.
  • re.fullmatch(r"PAY-\d+", "PAY-2291x") is None, but /PAY-\d+/.test("PAY-2291x") is true, because it finds PAY-2291 inside. /^PAY-\d+$/.test("PAY-2291x") is false. isdigit() ports the same way: /^\d+$/.test("2291") is true.
  • includes() and startsWith() don’t take a pattern. Pass one, and JavaScript stops with TypeError: First argument to String.prototype.includes must not be a regular expression. Test with a caret instead: test() with ^ does what startsWith() 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:

Language: JavaScript
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:

Language: Program Output
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.

00:00 A port of the release notes runs without an error, but it prints one ticket line too many. It announces PAY-2288, closing quote and all, from the commit that reverted it.

00:12 And the revert is missing under Other commits. That line comes from the script’s first question: is this a ticket commit? In this lesson, you’ll port that question and find where the extra yes comes from.

00:28 Here’s that question on its own, over the week’s seven subjects. The Python compiles the ticket pattern once: a key like PAY-2330, a colon and a space, then the title.

00:41 re.IGNORECASE lets the lowercase key through too. The loop counts each kind. match() returns a match object or None, and the if reads them as yes and no.

00:54 Python prints 4 ticket commits and 3 other commits.

01:00 JavaScript has no re module. A regular expression is a value of its own, already compiled, written between slashes. No quotes and no r prefix: a pattern isn’t a string, so a backslash is never a string escape.

01:16 The flag goes after the closing slash: i, for ignore case, as re.IGNORECASE is in Python. On ASCII text, the pattern reads as in Python.

01:29 Backslash d, w and s, character classes, quantifiers, groups in parentheses and alternation all carry over. The script reuses the pattern, so it keeps a name in capitals.

01:44 A pattern answers yes or no through its test() method. It gives true for the first subject, and false for the stripe bump.

01:53 There’s no match object, only the answer. The loop ports line for line, with test() where Python called match() before. Python counted 4 ticket commits.

02:05 How many does the port count? Your turn: pause the video and predict.

02:15 It counts 5. One more ticket commit, from the same pattern, and no error.

02:24 The extra yes is the revert. Python’s match() only tries at the start of the text. The revert’s subject starts with Revert, so match() returns None for it.

02:36 test() searches anywhere, as re.search() does. It finds a ticket key at position 8, inside the quotes. Python’s search() would count 5 as well.

02:48 The pattern is the same: what changed is where the function lets it match.

02:55 JavaScript has one way to test, and it searches, so you write where the match must start into the pattern. A caret at the front means the start of the text: the anchor Python’s match() adds for you.

03:09 Run it again. 4 ticket commits and 3 others, as in Python. The revert now fails the test. In the full script, it lands under Other commits, and the release notes match Python’s.

03:25 One rule covers Python’s three functions. re.search() is the pattern as it is. re.match() adds a caret. re.fullmatch() adds a caret and a dollar sign, the end of the text.

03:40 Take a key with a stray letter. Python’s fullmatch() rejects it. Without anchors, the test passes: it finds the key inside. With both anchors, it fails.

03:54 isdigit() ports the same way. One warning: pass a pattern to includes() or startsWith(), and JavaScript stops with a type error.

04:05 Test with a caret instead: it does what startsWith() would.

04:11 Write a fixed pattern as a literal, and call its test() method. It always searches, so the anchors decide what you’re checking. Without them, the text contains a match.

04:22 With both, the text is one. To check a value, try it with extra characters on each side. Next up is Building Patterns With new RegExp(), a text lesson. There, you’ll build the ticket pattern from the team’s setting, so other teams can use their own key.

Become a Member to join the conversation.