Building Patterns With new RegExp()

With ^ in front, the ticket test counts four ticket commits and three others, as Python does. But the script doesn’t hard-code PAY: other teams run it with their own key, so it builds the pattern from a setting, and a regex literal can’t hold a variable.

Build the Pattern From the Team Setting

Python builds the pattern with a raw f-string:

Language: Python
import re

TEAM = "PAY"
TICKET = re.compile(rf"({TEAM}-\d+): (.+)", re.IGNORECASE)

The Python code has no ^ because it calls TICKET.match(), which anchors at the start. The port keeps the last lesson’s ^, and each other piece of that line has a JavaScript counterpart:

Python JavaScript
re.compile(text, flags) new RegExp(text, flags)
re.IGNORECASE "i", a string of flag letters
f"...{TEAM}..." `...${TEAM}...`
TICKET.flags, a number TICKET.flags, the letters ("i")

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

Become a Member to join the conversation.