WebToolsPlanet
developer Tools

Regex Visualizer

Visualize regular expressions as interactive railroad diagrams — understand complex patterns with a clear SVG flowchart, live match testing, and flag explanations.

Last updated: March 25, 2026

Used 28K+ times
Client-Side Processing
Input Data Stays on Device
Instant Local Execution

What users say

The railroad diagram makes complex patterns instantly readable. I use the SVG export in pull request descriptions to explain regex validation logic to reviewers.
Sam K.Senior Developer

Find this tool useful? Support the project to keep it free!

Buy me a coffee

What is Regex Visualizer?

A regular expression (regex) is a compact pattern language for matching text — but regex syntax is famously dense and hard to read at a glance. A single expression like `^(?:https?:\/\/)?(?:www\.)?([\w-]+)\.[a-z]{2,}(?:\/.*)?$` can take minutes to parse manually. A railroad diagram (also called a syntax diagram) translates this into a visual flowchart where each path through the diagram represents a valid match, making even complex patterns immediately understandable.

Railroad diagrams were originally used in the 1960s–70s to document programming language grammars (the Pascal language reference was famous for its railroad diagrams). Applied to regex, each component becomes a box you follow left-to-right: literals appear as rounded boxes, character classes as bracketed nodes, groups as nested paths, quantifiers as loops, and alternation (`|`) as branching parallel paths. The visual structure instantly communicates grouping priority, quantifier scope, and optional vs required components — information that's buried in symbols in the raw regex string.

How to Use Regex Visualizer

1

Type or paste a regex pattern into the input field (without surrounding / slashes)

2

Optionally toggle flags using the flag buttons: g (global), i (case insensitive), m (multiline), s (dotAll), u (unicode)

3

The railroad diagram renders live — follow the arrows from left to right to understand the pattern's structure

4

Paste a test string in the "Test Input" panel to see highlighted matches and capture group values

5

Hover over any diagram node to see a plain-English tooltip explaining what that component matches

Common Use Cases

  • Debugging a complex regex that matches fewer (or more) strings than expected by seeing the diagram's logical structure
  • Writing documentation for a regex-heavy codebase by exporting the railroad diagram as an SVG image
  • Learning regex by writing patterns and seeing how each added character changes the diagram
  • Explaining a regex in a code review — share the diagram URL instead of annotating the raw pattern
  • Understanding a legacy codebase's validation regex patterns without decoding them manually
  • Teaching regular expressions in a blog post, workshop, or video tutorial with visual aids
  • Verifying that alternation branches (a|b) have the correct scope — are they inside or outside a group?
  • Checking quantifier scope: does * apply to the whole group or just the last character?

Example Input and Output

Visualizing a URL validation regex in railroad diagram form:

Regex pattern
Pattern: ^(https?)://([\w-]+\.)+[a-z]{2,6}(/[\w.-]*)*/?$
Flags: i (case-insensitive)
Railroad diagram structure (described)
Railroad diagram (left-to-right flow):

START ──► ^ (line start)
    ──► Capture Group 1:
        ├── "https"
        └── "http"
    ──► "://"
    ──► Capture Group 2 (one or more):
        └── [word chars or hyphen] + "."
    ──► [a-z] (2 to 6 times)
    ──► Capture Group 3 (zero or more):
        └── "/" + [word chars, dots, hyphens]*
    ──► "/" (optional)
    ──► $ (line end) ──► END

Test: "https://www.example.co.uk/path/file.pdf" → ✅ Match

Client-Side Processing

All regex parsing and SVG diagram generation runs in your browser. Patterns — which may contain proprietary business logic or sensitive validation rules — are never sent to any server.

Catastrophic Backtracking

Some regex patterns cause exponential execution time (ReDoS — Regular Expression Denial of Service). The visual diagram helps identify dangerous patterns: nested quantifiers inside groups ((a+)+ or (a|a)+) create exponential backtrack trees. In production code, test suspicious regexes (especially from user input) against long adversarial strings. Use atomic groups or possessive quantifiers (supported in Python/Java, not JavaScript) to prevent backtracking where needed.

JS vs PCRE Regex Differences

This visualizer uses JavaScript's RegExp engine. PCRE (PHP, Nginx, Python, Perl) and Java/Ruby regex engines have additional features: (1) Possessive quantifiers (++, *+) — JS has no equivalent. (2) Recursive patterns (?R) — not in JS. (3) Branch reset groups (?|...) — not in JS. (4) (?(condition)yes|no) conditional patterns — not in JS. For cross-engine regex development, test patterns in each target language runtime separately.

Frequently Asked Questions

What regex syntax is supported by this visualizer?
The visualizer supports JavaScript RegExp syntax: character classes ([a-z], [\w], \d, \s), quantifiers (*, +, ?, {n}, {n,m}), groups (capturing (abc), non-capturing (?:abc), named (?<name>abc)), alternation (a|b), anchors (^, $, \b, \B), lookahead (?=abc), negative lookahead (?!abc), lookbehind (?<=abc), negative lookbehind (?<!abc), and backreferences (\1, \k<name>). Flags: g, i, m, s (dotAll), u (unicode), v (unicodeSets).
What are the regex flags and what do they do?
g (global): find all matches, not just the first. i (ignoreCase): match [A-Z] and [a-z] interchangeably. m (multiline): ^ and $ match the start/end of each line, not just the whole string. s (dotAll): . matches newline characters (\n) in addition to all other characters. u (unicode): enables full Unicode support for \u{...} escapes and Unicode property escapes (\p{L}). v (unicodeSets, ES2024): enables set notation inside character classes like [\p{Letter}--\p{ASCII}].
How do I match a literal period or parenthesis in a regex?
Special regex characters must be escaped with a backslash: \. matches a literal period, \( and \) match literal parentheses, \+ matches a literal plus, \* matches a literal asterisk. In JavaScript strings (when using new RegExp(string)), backslashes themselves must be doubled: "\\." in a string literal produces the regex \. pattern. The railroad diagram clearly shows whether a period is a pattern (any character) or a literal escaped period \. (specific character).
What is the difference between greedy and lazy quantifiers?
Greedy quantifiers (*, +, {n,m}) match as many characters as possible while still allowing the overall pattern to succeed. Lazy quantifiers (*?, +?, {n,m}?) match as few characters as possible. Example: for the string "ab cd ef", the pattern ".+" (greedy) matches the entire string "ab cd ef". The pattern ".+?" (lazy) matches "a" (the shortest possible match). Add ? after any quantifier to make it lazy. The railroad diagram shows lazy quantifiers with a visible "prefer fewer" annotation.
How does alternation (|) priority work?
The alternation operator (|) has the lowest precedence in regex — it applies to everything on its left vs. everything on its right within the current scope. In "cat|dog" it matches "cat" OR "dog". In "ca(t|d)og" the alternation is scoped to the group: matches "cato" + "dog". The railroad diagram makes this immediately clear: alternation inside a group shows as branches inside the group box, while outer alternation creates two fully separate paths through the diagram.
Can I share my regex with the railroad diagram?
Yes. The page URL auto-updates with your regex encoded in the URL hash/query parameter, so you can copy and share the URL directly. For shareable diagrams that don't require loading the web page: export the diagram as SVG (vector, scales perfectly) or PNG for embedding in documentation, pull request comments, Notion pages, or Confluence. Regexper (regexper.com) is another dedicated railroad diagram tool with a canonical shareable URL format.

How This Tool Works

The regex string is parsed using a JavaScript RegExp AST parser (based on the regexpp or regjsparser library). The AST nodes (Literal, CharacterClass, Quantifier, Group, Alternative, Assertion, etc.) are recursively mapped to railroad diagram components. Each node type produces a different SVG element: literals render as rounded rectangles; character classes as brackets; quantifiers as loop arrows; groups as nested bounding boxes; alternation as parallel branch paths connected by junction points. The SVG is rendered inline and scaled with the viewport.

Technical Stack

regexpp / regjsparserSVG railroad diagram rendererJavaScript RegExp ASTClient-side only