Regex Visualizer
Visualize regex as a railroad diagram with explanations.Runs in this tab.⌘ ↵ runs it
Enter a regex pattern above to see its railroad diagram and explanation.
Next step
Turn a click-through into an interactive demo
AtriumKit records your web app once in Chrome and turns every click into a step of a demo you can embed or share. There’s a free plan.
Try AtriumKit freeAbout Regex Visualizer
How to read a regex railroad diagram
Start at the dot on the left and follow the line to the dot on the right. Every path you can trace spells out a string the pattern matches.
| Shape | Meaning |
|---|---|
| Rounded box | One literal character, ., or an escape such as \d or \.. cat is three boxes |
| Square-cornered box | A character class, shown as written, e.g. [0-9A-Za-z.-] |
| Diamond | An anchor, ^ or $ |
Dashed frame labeled Group #n |
Capturing group n, the number a backreference uses. A frame labeled just Group is (?:…) |
| Loop underneath, with a label | A quantifier: 1+, 0+, 0-1, {4}, {8,}, {2,4}, with lazy added for +?, *? and so on |
| Stacked rows joined by curves | Alternation: the match takes exactly one row |
Optional elements get a loop labeled 0-1, not a separate bypass track, so read the label to tell ? from +.
Worked example: a semver regex
^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?$
Reading left to right, the diagram shows:
- A
^diamond. - Group #1 around a
\dbox with a1+loop. - A
\.box. - Groups #2 and #3, separated by another
\.box and built the same way. - A dashed
Groupframe with a0-1loop, containing a-box and Group #4. Group #4 wraps[0-9A-Za-z.-]with a1+loop. - A
$diamond.
The English explanation follows the same nesting:
1. Start of string
2. Capturing group #1:
3. One or more of: A digit (0-9)
4. Literal '.'
…
10. Optionally of:
11. Non-capturing group:
12. Literal character '-'
13. Capturing group #4:
14. One or more of: one of [0-9A-Za-z.-]
15. End of string
Tracing the paths shows what the pattern accepts and what it misses. 1.2.3 and 1.2.3-beta.1 match, and 1.2 doesn’t. Nothing in the diagram blocks leading zeros, so 01.2.3 passes even though SemVer forbids it. There’s also no path for build metadata, so 1.2.3+build.5 fails. For real version parsing and comparison, use the semver calculator.
The test box always matches with the g flag alone, without m, so ^ and $ anchor to the whole box rather than to each line. Test one version at a time, or switch to the regex tester and turn on multiline.
Spotting bugs by shape
Anchors inside an alternation. ^cat|dog$ draws two rows, ^ c a t and d o g $. Each anchor applies to only one branch, so both catalog and hotdog match. ^(?:cat|dog)$ puts both diamonds outside the stack.
No diamonds at the ends. Without anchors, a pattern matches anywhere in the input: \d{3}-\d{4} finds 555-1234 inside call 555-12345 now. A validation pattern should have a diamond at each end.
A bare dot box. In ^\d+.\d+$ the middle box is ., which the explanation reads as “Any character (except newline)”, so 1x2 passes. Escape it as \. and the explanation reads “Literal ‘.’”.
A loop inside a loop. ^(a+)+$ draws Group #1 with a 1+ loop around an a box that has its own 1+ loop. When the inner and outer loops can consume the same characters, a failing input forces the engine to try every way of splitting the run between them. In Node 24, testing 'a'.repeat(30) + '!' against ^(a+)+$ took about 7 seconds, and each extra a roughly doubled the time. The same input against ^a+$ finished in well under a millisecond. ^(\w+\s?)+$ and ^(\d+|\d+\.\d+)*$ have the same shape and the same problem. Drawing the diagram never runs the regex, so you can safely inspect a dangerous pattern here. The test box does run it, though, so a long failing input there will freeze the tab.
What the parser understands
The visualizer uses its own small parser, not the JavaScript engine. It handles:
- literals,
., and the anchors^and$ - character classes, including negated ones. Common ranges like
a-z,0-9anda-zA-Z0-9get named in the explanation - escapes:
\d \D \w \W \s \S \b \B \n \r \t \0are described, and any other escaped character is read as that literal - capturing groups,
(?:…)groups and alternation - the quantifiers
* + ? {n} {n,} {n,m}and their lazy forms
The gaps:
- Lookarounds and named groups (
(?=…),(?!…),(?<=…),(?<!…),(?<name>…)) are drawn as numbered capturing groups whose first boxes are the literal characters?,=,<and so on. Lookarounds also take a group number, so every group after one is numbered higher than JavaScript would number it. - Backreferences such as
\1get an escape box, but the explanation calls them “Literal ‘1’”. - Unicode property escapes: in
\p{L}, the{L}is read as a repeat count.
For patterns that use these, rely on the diagram for overall structure and check the actual matching in the regex tester. To compare with another engine’s parse, Python can print its parse tree:
python3 -c "import re; re.compile(r'^(?:cat|dog)$', re.DEBUG)"
# AT AT_BEGINNING
# BRANCH
# LITERAL 99
# ...
Python’s syntax differs from JavaScript’s in places, so treat that as a cross-check, not a verdict. For writing and debugging patterns against sample text, see how to test regex online.
Questions
What is a railroad diagram?
Does it support all regex features?
How is the regex visualizer different from the regex tester?
Can I use the regex visualizer to learn regular expressions?
Does the regex visualizer send my pattern to a server?
Comments
More in Text & String Utilities
- Word Counter
- Regex Tester
- String Case Converter
- Slug Generator
- Text Diff
- Lorem Ipsum Generator
- Line Sorter & Deduplicator
- Text Reverse
Comments