Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Looking Behind and Ahead

Looking Behind and Ahead

In this lesson, you pull amounts out of a ticket note with patterns that check the text around a match without taking it in. The refund ticket PAY-2401 carries a note with thirty dollars, then twenty-five euros, and seven dollars, and the refund script needs the dollar amounts as bare numbers.

In Python, you’d reach for findall() with a group around the digits. The pattern matches a dollar sign and its digits, and findall() hands back only what the group caught:

Language: Python
import re

note = "Total: $30, then €25 and $7"

print(re.findall(r"\$(\d+)", note))

This prints ['30', '7']. JavaScript has no findall(). With the g flag, short for global, .match() returns every match instead. So the line-by-line port keeps the same pattern, group and all, inside a regex literal, the pattern between slashes and the flags after the closing one:

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

00:00 In this lesson, you’ll pull amounts out of a ticket note with patterns that check the text around a match without taking it in. The refund ticket PAY-2401 carries this note: thirty dollars, then twenty-five euros, and seven dollars.

00:15 The refund script needs the dollar amounts as bare numbers. In Python, you’d reach for findall() with a group around the digits. The pattern matches a dollar sign and its digits, and findall() hands back only what the group caught: thirty and seven.

00:34 JavaScript has no findall(). With the g flag, short for global, .match() returns every match instead. So the port keeps the same pattern, group and all, inside a regex literal: the pattern between slashes, the flags after the closing one.

00:51 What does this line log? Your turn: pause the video and predict.

01:01 It logs the dollar signs too: dollar thirty and dollar seven.

01:08 Both languages start the match at the dollar sign. Mark the boundaries on the note: to reach the digits, the pattern steps over the dollar sign at index seven, so the first match runs from there through the zero of thirty.

01:22 Python’s findall() then hands back only the group. .match() with g has no such switch: it always hands back the whole match.

01:32 You could loop over .matchAll() and read each match’s group, but when the dollar sign only needs checking, keep it out of the match.

01:42 The team lead’s Python version had no group. It put the dollar sign in a lookbehind. JavaScript spells a lookbehind the same way, so the line ports unchanged.

01:52 This lookbehind checks for a dollar sign right before the match position, and adds nothing to the match. So the first match starts at index eight, after the dollar sign.

02:03 The log shows thirty and seven, as Python’s does. That’s how JavaScript developers write it: the context in a lookbehind, and .match() with g for the values.

02:15 The analogy stops at length: JavaScript accepts a lookbehind of any length, such as a dollar sign and any spaces, and Python’s re rejects it with a PatternError.

02:27 A lookbehind can also say what must not come before. Swap the equals sign for an exclamation mark, and you get a negative lookbehind. Its class says no dollar sign and no digit may sit right before the match.

02:42 The log shows one amount, the twenty-five euros, in both languages.

02:47 Why the digits? As in re, with only the dollar sign, the zero of thirty would match too, because a three sits before it. A lookbehind checks only the text that ends right where the match starts, never anywhere earlier.

03:04 Lookaheads work the same way, facing forward. The ticket also lists the files the fix changed. For the JavaScript files’ names, match a word, then check with a lookahead that a dot and js come next.

03:19 The log shows branch and slug, with no extension, because the lookahead takes nothing in. Python’s re prints the same list.

03:29 For the files the port still has to replace, a negative lookahead says what must not come next. This pattern matches a name, a dot and an extension, as long as the extension doesn’t start with js.

03:43 The lookahead only checks the extension, and the second word pattern then reads it into the match. Only the Python file passes, in both languages.

03:54 So looking behind starts with a less-than sign, and looking ahead doesn’t. Either way, an equals sign means “must”, and an exclamation mark “must not”. The spelling is Python’s, and a lookaround itself adds nothing to the match.

04:11 Next up is the text lesson Scanning Text With lastIndex and the y Flag. There, you’ll see how a g or y regex carries its position in lastIndex, and scan a ticket title with y, JavaScript’s answer to Python’s pattern.match() with a start position.

Become a Member to join the conversation.