Skip to content

Regex Visualizer

Embed

Paste this where you want Regex Visualizer on your site. Adjust the height to fit.

<iframe src="https://www.browserutils.dev/embed/regex-visualizer/" width="100%" height="500" frameborder="0" loading="lazy" title="Regex Visualizer"></iframe>
<p style="font-size:12px;margin:4px 0 0"><a href="https://www.browserutils.dev/tools/regex-visualizer/">Regex Visualizer</a> by BrowserUtils</p>

Visualize regex as a railroad diagram with explanations.Runs in this tab.

//g

Enter a regex pattern above to see its railroad diagram and explanation.

Next step

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

  1. A ^ diamond.
  2. Group #1 around a \d box with a 1+ loop.
  3. A \. box.
  4. Groups #2 and #3, separated by another \. box and built the same way.
  5. A dashed Group frame with a 0-1 loop, containing a - box and Group #4. Group #4 wraps [0-9A-Za-z.-] with a 1+ loop.
  6. 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-9 and a-zA-Z0-9 get named in the explanation
  • escapes: \d \D \w \W \s \S \b \B \n \r \t \0 are 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 \1 get 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?
A railroad diagram is a visual representation of a grammar or pattern. For regex, it shows the flow of matching as a diagram with paths for alternatives, loops for quantifiers, and labeled boxes for character classes and literals.
Does it support all regex features?
The visualizer supports common regex features including literals, character classes, quantifiers, groups, alternation, and anchors. Some advanced features like backreferences have simplified representations.
How is the regex visualizer different from the regex tester?
The regex tester highlights matches in sample text for debugging. The visualizer generates a railroad diagram that shows the structure and flow of the pattern itself, which helps you understand how the regex engine processes it.
Can I use the regex visualizer to learn regular expressions?
Yes, the visual diagram and plain-English explanation make it easier to understand how regex patterns work, especially for beginners who find raw regex syntax difficult to read.
Does the regex visualizer send my pattern to a server?
No, the diagram is generated entirely in your browser. Your regex patterns are never transmitted to any external service.

More in Text & String Utilities

All 28 text & string utilities