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