Testing patterns in regex101 and the console
The working loop for every pattern: write it in a tester, run it on input that should match and input that should not, and check it in the flavour you ship.
Testing patterns in regex101 and the console
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- One matching example proves almost nothing. Test every pattern against strings that should match and strings that must not.
- A tester like regex101 shows matches, an explanation of each token and the flavour in use. You still have to confirm the result in the engine you will ship on.
- In a terminal, node -p, a Node REPL, grep -nE and pcre2grep give a quick loop with no browser.
- Unanchored patterns match inside longer strings. /\d{3}-\d{4}/ accepts 1555-01423 and 'call 555-0142 now' until you add ^ and $.
- The same pattern can behave differently across flavours. (?<year>\d{4}) works in JavaScript and Perl, fails in Python 3.9, and \d works in BSD grep but not in BSD sed.
One example proves nothing
You write /\d{3}-\d{4}/ for a seven-digit phone number, paste 555-0142, and it matches. Done?
It isn’t, and the reason is the most useful habit in this roadmap. A pattern is a claim about every string you could ever feed it. One matching string checks one tiny corner of that claim. To find the bugs you need strings that should not match. Here is a short harness, check, that takes a list of each:
function check(re, yes, no) {
let bad = 0;
for (const s of yes) if (!re.test(s)) { console.log("MISSED ", JSON.stringify(s)); bad++; }
for (const s of no) if (re.test(s)) { console.log("FALSE HIT", JSON.stringify(s)); bad++; }
console.log(String(re), bad ? bad + " problem(s)" : "ok");
}
const yes = ["555-0142", "000-0000"];
const no = ["5550142", "555-014", "1555-01423", "call 555-0142 now"];
check(/\d{3}-\d{4}/, yes, no);
check(/^\d{3}-\d{4}$/, yes, no);
Output from Node 26:
FALSE HIT "1555-01423"
FALSE HIT "call 555-0142 now"
/\d{3}-\d{4}/ 2 problem(s)
/^\d{3}-\d{4}$/ ok
The first pattern accepted two strings that are not phone numbers. It found 555-0142 inside each one, because nothing told it to stop at the edges. The second pattern adds ^ and $, which pin it to the start and the end, and passes. Those anchors get their own lesson. For now, keep the habit: write the “should match” and “should not match” lists first, and run the pattern against both.
The tester: regex101
A web tester shows a pattern, some test text and the matches highlighted live. That beats guessing, and it is the quickest way to see why something does not match. regex101 is the one most people use. From web guides to it and from the video’s title, it also gives a token-by-token explanation of your pattern, a list of match details, a switch between flavours, and a debugger that steps through the match. I did not open regex101 from here, so I have not checked any of that, or which flavours it offers.
The flavour switch is the part to remember. A pattern that passes under one flavour is only proven for that flavour. If you will run it in JavaScript, set the tester to ECMAScript before you trust it.
The terminal loop
Sometimes the browser is overhead. In a terminal you have these:
$ node -p '"a1b22c333".match(/\d+/g)'
[ '1', '22', '333' ]
$ printf '555-0142\n5550142\n1555-01423\n' | /usr/bin/grep -nE '^[0-9]{3}-[0-9]{4}$'
1:555-0142
node -p prints the value of an expression. Running node with no arguments opens a REPL, a read-eval-print loop, where you type /a.c/.test("abc") and get true back. I fed two lines to node -i and it answered true and [ '2026', '03', '14' ]. grep -nE shows matching lines with their numbers, so the last line shows that only the first of three inputs passed the anchored pattern. Both are quick to reach for in the middle of other work.
Check the flavour you ship
The tester and the target can disagree. The same named-group pattern gave different results in four engines on this machine:
$ node -e 'console.log("2026-03-14".match(/(?<year>\d{4})-(?<month>\d{2})/).groups)'
[Object: null prototype] { year: '2026', month: '03' }
$ python3 -I -c 'import re; re.search(r"(?<year>\d{4})", "2026")'
re.error: unknown extension ?<y at position 1
$ python3 -I -c 'import re; print(re.search(r"(?P<year>\d{4})", "2026").group("year"))'
2026
$ perl -e 'print "2026-03-14" =~ /(?<year>\d{4})/ ? "perl: $+{year}\n" : "no\n"'
perl: 2026
JavaScript and Perl accept (?<year>...). Python 3.9 raises an error and wants (?P<year>...). And on the shell side:
$ echo 'abc 123' | /usr/bin/grep -oE '\d+'
123
$ echo '12 d' | sed -E 's/\d/X/'
12 X
On this machine, \d worked as “digit” in macOS’s BSD grep, which here is version 2.6.0-FreeBSD, and BSD sed -E treated \d as the plain letter d. The POSIX classes are the portable choice: [[:digit:]] and [0-9] behaved the same in both. Shorthand classes goes through what each tool supports.
About the video
Federico Terzi’s “Regex101 - The Ultimate Tool for Regular Expressions - DevProTips #1 - Regex Visualizer and Debugger” was published on 28 May 2020 and runs 3 minutes 49 seconds. The description links to an article about why email regexes are hard and says nothing else about content. It has no chapters. The title says the video covers regex101 as a visualizer and debugger. I couldn’t read a transcript, so I can’t say which features it shows or which flavour it picks. It is short enough to watch before opening the site yourself. The lesson’s test-case habit, the terminal loop and the flavour comparison are not in the video as far as I can tell from its metadata. English per its title and description.
What I did not run
regex101 itself: the flavour switch, the explanation panel and the debugger are described from web guides and a video title, not from use. I also did not run GNU grep or a browser console.
Next
With a loop you can trust, the next lesson pins down what “flavour” means and which parts of a pattern are the same everywhere: Flavors and the common core.
Check yourself
Your pattern matches the one sample you tried. What else should you test before trusting it?
Strings it should reject, and near misses such as too short, too long, extra text around it and the wrong separator. A false hit is how bad patterns reach production.
Why does /\d{3}-\d{4}/ accept 'call 555-0142 now'?
Nothing in the pattern ties it to the start or end of the input, so it matches the substring 555-0142 inside it. Adding ^ and $ makes the whole string match.
You test a pattern in regex101 with the PCRE flavour and ship it in Python. What can go wrong?
The flavours differ. Python 3.9 rejects (?<name>...) and needs (?P<name>...), and escape and flag handling differ too. Switch the tester to the target flavour and re-run your test cases.
Why test in the same engine you ship in, even after regex101 passes?
Because the tester's flavour may not match your runtime's version or flags. A one-line run in Node, Python or grep confirms the real behaviour.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.