Reading Matches With match()

Your ticket pattern now follows the team setting, and .test() tells you which commits have tickets. The release notes need more from each subject, though. Each ticket line prints the key and the title, so the script has to pull both out of the subject, and it has to say clearly when a subject holds no ticket at all.

In the Python script, you use a match object and its groups. In this lesson, you’ll read the same parts in JavaScript, port the “no match” check, and collect every ticket a subject mentions. One of those ports looks finished but isn’t, and you’ll want to know why before the whole script depends on it.

Pull Out the Key and the Title

The Python script calls .match() on the compiled pattern and reads each group by its number:

Language: Python
import re

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

subject = "PAY-2317: Add the SEPA mandate to receipts"
ticket = TICKET.match(subject)
print(ticket.group(1))
print(ticket.group(2))

It prints PAY-2317 and then Add the SEPA mandate to receipts.

JavaScript has a .match() method too, but it lives on the string and takes the pattern as its argument: subject.match(TICKET). Watch out for Python’s order here, because TICKET.match(subject) stops with TypeError: TICKET.match is not a function. Here’s the port, with the TICKET you built from TEAM and the whole result logged:

Language: JavaScript
const TEAM = "PAY";
const TICKET = new RegExp(String.raw`^(${TEAM}-\d+): (.+)`, "i");

const subject = "PAY-2317: Add the SEPA mandate to receipts";
const ticket = subject.match(TICKET);
console.log(ticket);

The match result is an array with a few labeled extras:

Language: Program Output
[
  'PAY-2317: Add the SEPA mandate to receipts',
  'PAY-2317',
  'Add the SEPA mandate to receipts',
  index: 0,
  input: 'PAY-2317: Add the SEPA mandate to receipts',
  groups: undefined
]

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.