Replacing Every Match With the g Flag

Your port reads the whole log by name now. Before a ticket line reaches the team chat, though, the Python script rewrites two parts of it with re.sub(). It tidies PAY-2330’s title, committed as Payout report totals off by a cent with two double spaces, and it turns 2026-11-06 into 06/11. The first port of the title runs without errors but still prints the line incorrectly.

Collapse the Spaces With the g Flag

In Python, one call squeezes every run of whitespace, and the padded title leaves two spaces before the hash:

Language: Python
import re

title = "Payout  report  totals off by a cent"
tidy = re.sub(r"\s+", " ", title).strip()
print(f"06/11  PAY-2330  {tidy:<36}0d9a6c3")

JavaScript has no re.sub(). The .replace() you know from fixed text takes a regex too, and .trim() does what a no-argument .strip() does for ordinary spaces, tabs, and newlines. The pattern has no flags in Python, so a line-by-line port has none either. Predict how its line compares with PAY-2317’s:

Language: JavaScript
const title = "Payout  report  totals off by a cent";
const tidy = title.replace(/\s+/, " ").trim();
const sepa = "Add the SEPA mandate to receipts";

console.log(`06/11  PAY-2330  ${tidy.padEnd(36)}0d9a6c3`);
console.log(`04/11  PAY-2317  ${sepa.padEnd(36)}c2d4a61`);

There’s no error, and the hash still lines up, but one double space survives in the title:

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.