Regex Railroad Diagram Visualizer

Render a regular expression as an SVG railroad (syntax) diagram showing literals, groups, alternations, and repetition, with capture-group numbering and flag explanations

Parses a JavaScript/PCRE-flavored regex with the ret library into an AST, then maps each node to a railroad-diagram primitive (rounded boxes for literals, labeled groups for captures, choice branches for alternations, loop-back arrows for quantifiers) rendered as SVG via railroad-diagrams. Also lists every capture group (with name) and explains each flag. Useful for teaching regex, documenting patterns, and spotting nested-quantifier backtracking risks.

Example Results

2 examples

Visualize a URL-matching regex

A common URL pattern rendered as a railroad diagram with anchors, groups, and an optional port.

Railroad diagram with capture groups and flag explanation.
View input parameters
{ "pattern": "^(https?)://([^/:]+)(:\\d+)?(/.*)?$", "flags": "i" }

Visualize an email-capture pattern

A simple email capture group with word boundaries rendered as a diagram.

Email pattern railroad diagram with capture group.
View input parameters
{ "pattern": "\\b(\\w+[\\w.-]*@\\w+[\\w.-]*\\.\\w+)\\b", "flags": "gi" }

Key Facts

Category
Developer & Web
Input Types
text
Output Type
html
Sample Coverage
4
API Ready
Yes

Overview

The Regex Railroad Diagram Visualizer converts complex JavaScript and PCRE-flavored regular expressions into clean, interactive SVG syntax diagrams. By parsing your regex pattern into an Abstract Syntax Tree (AST), the tool visually maps literals, capture groups, alternations, and quantifiers into an easy-to-read flowchart, complete with capture-group numbering and flag explanations.

When to Use

  • When documenting complex regular expressions in technical specifications or codebase readmes.
  • When debugging nested quantifiers, alternations, or capture groups to prevent performance issues like catastrophic backtracking.
  • When teaching or learning regular expression syntax and wanting to visualize how the engine parses a pattern.

How It Works

  • Enter your JavaScript or PCRE-compatible regular expression pattern and optional flags into the input fields.
  • The tool parses the pattern into an Abstract Syntax Tree (AST) using the ret library.
  • Each AST node is mapped to a visual railroad primitive, such as rounded boxes for literals, choice branches for alternations, and loop-back arrows for quantifiers.
  • The visualizer outputs a self-contained SVG diagram along with a detailed list of numbered capture groups and flag explanations.

Use Cases

Visualizing complex URL routing patterns to verify capture groups and optional parameters.
Documenting email validation patterns in team wikis using clear, graphical syntax diagrams.
Auditing legacy regex patterns for nested loops that could cause performance bottlenecks.

Examples

1. Visualizing a URL-matching regex

Backend Developer
Background
A developer needs to document a complex URL routing regex pattern for an API gateway to ensure team members understand the path parameters.
Problem
The regex pattern is difficult to read and explain in plain text.
How to Use
Input the URL pattern into the Regular Expression field and set the flags to i.
Example Config
Pattern: ^(https?)://([^/:]+)(:\d+)?(/.*)?$
Flags: i
Outcome
An SVG diagram is generated showing clear branches for optional protocols, hostnames, ports, and paths, alongside a list of the four numbered capture groups.

2. Deconstructing an Email Capture Pattern

QA Engineer
Background
A QA engineer wants to verify that an email validation regex correctly captures the domain and username parts without backtracking issues.
Problem
Verifying the boundary conditions and character classes of a complex email regex manually is error-prone.
How to Use
Paste the email regex pattern into the input field and add the gi flags.
Example Config
Pattern: \b(\w+[\w.-]*@\w+[\w.-]*\.\w+)\b
Flags: gi
Outcome
The tool renders a railroad diagram highlighting the word boundaries, character repetitions, and the main capture group, making the matching logic instantly clear.

Try with Samples

image, svg, regex

Related Hubs

FAQ

What regex flavors are supported?

The visualizer supports JavaScript and PCRE-flavored regular expressions.

Can it identify named capture groups?

Yes, it lists and labels all capture groups, including named groups and their corresponding indices.

Does the tool support advanced PCRE features like recursion?

No, advanced features like recursive patterns, branch resets, and conditional groups are not supported and will return a parsing error.

How do I read the generated railroad diagram?

Read the diagram from left to right, following the paths through literals, branches, and loop-back arrows for repetitions.

Can I export the diagram?

Yes, the diagram is rendered as a standard SVG element that you can copy or save directly from your browser.

API Documentation

Request Endpoint

POST /en/api/tools/regex-railroad-diagram-visualizer

Request Parameters

Parameter Name Type Required Description
pattern text Yes -
flags text No -

Response Format

{
  "result": "
Processed HTML content
", "error": "Error message (optional)", "message": "Notification message (optional)", "metadata": { "key": "value" } }
HTML: HTML

AI MCP Documentation

Add this tool to your MCP server configuration:

{
  "mcpServers": {
    "elysiatools-regex-railroad-diagram-visualizer": {
      "name": "regex-railroad-diagram-visualizer",
      "description": "Render a regular expression as an SVG railroad (syntax) diagram showing literals, groups, alternations, and repetition, with capture-group numbering and flag explanations",
      "baseUrl": "https://elysiatools.com/mcp/sse?toolId=regex-railroad-diagram-visualizer",
      "command": "",
      "args": [],
      "env": {},
      "isActive": true,
      "type": "sse"
    }
  }
}

You can chain multiple tools, e.g.: `https://elysiatools.com/mcp/sse?toolId=png-to-webp,jpg-to-webp,gif-to-webp`, max 20 tools.

If you encounter any issues, please contact us at [email protected]