A TypeScript library and CLI tool to visualize regular expressions as Mermaid flowchart diagrams.
For example, visualise this:
^(?<protocol>https?:\/\/)?(?<domain>[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})(?<path>\/.*)?$as this:
View this in the Mermaid Live Editor
- π Visual Regex Understanding - Convert complex regex patterns into intuitive flowcharts
- π Wide Support - Mermaid diagrams are embeddable in various tools like GitHub, GitLab, VS Code, Notion, Obsidian, Docusaurus and more
- π Easy Sharing - Share visual regex diagrams in documentation, presentations, or code reviews
- π¦ CLI & Library - Use as a command-line tool or integrate into your projects
- π Comprehensive Support - Handles capture groups, lookaheads, lookbehinds, and more
- π¨ Multiple Themes - Choose from default, neutral, dark, forest, or no styling
- β‘ Fast & Modern - Built with TypeScript as an ESM library for optimal performance
bun install -g regex-to-mermaidpnpm install -g regex-to-mermaidnpm install -g regex-to-mermaidyarn global add regex-to-mermaidBasic:
regex-to-mermaid 'foo|bar'All options:
regex-to-mermaid 'foo|bar' \
--theme default \
--direction LR \
--flavor auto \
--output diagram.mmd| Short | Argument | Description | Default |
|---|---|---|---|
-d |
--direction |
Diagram direction: LR (left-right) or TD (top-down) |
LR |
-f |
--flavor |
Regex flavor: regexp (ECMAScript), or auto (detect automatically) |
auto |
-t |
--theme |
Mermaid theme: default, neutral, dark, forest, or none |
default |
-o |
--output |
Output file (if not specified, outputs to stdout) | stdout |
Pipe to @mermaid-js/mermaid-cli to output an SVG or PNG image:
regex-to-mermaid 'foo|bar' | \
npx @mermaid-js/mermaid-cli \
--input - \
--output diagram.pngGenerate a shareable link to view and edit the diagram in the Mermaid Live Editor:
regex-to-mermaid 'foo|bar' | jq -Rscj '{code: .}' | gzip -n -c -9 | base64 -w0 | tr '/+' '_-' | awk '{printf "https://mermaid.live/edit#pako:%s\n", $0}'jq -Rscj '{code: .}'uses jq to create a minimal JSON state object with the Mermaid code-Rtells jq to treat the input as a raw string-sreads the entire input stream into a single string-censures the output JSON is on a single line-jjoins the output without a newline
gzip -n -c -9compresses the JSON with maximum compression-nomits the original filename and timestamp for consistent output-cwrites to standard output-9uses the highest compression level
base64 -w0encodes the compressed data in base64-w0disables line wrapping
tr '/+' '_-'makes the base64 URL-safeawk '{printf "https://mermaid.live/edit#pako:%s\n", $0}'constructs the final URL
import { regexToMermaid } from 'regex-to-mermaid';
const diagram = regexToMermaid('foo|bar');
console.log(diagram);function regexToMermaid(
pattern: string | RegExp,
options?: {
direction?: 'LR' | 'TD'; // Default: 'LR'
flavor?: 'regexp' | 'auto'; // Default: 'auto'
theme?: 'default' | 'neutral' | 'dark' | 'forest' | 'none'; // Default: 'default'
},
): string;See available THEMES.md.
| Flavor | Usage | Support |
|---|---|---|
| RegExp (ECMAScript) | JavaScript | β Supported, up to and including ES2025 |
| PCRE2 | PHP >= 7.3 | π§ Limited support |
| PCRE (Perl Compatible Regular Expressions) | PHP < 7.3, R | π§ Limited support |
| BRE (POSIX Basic) | sed, grep, etc | π§ Limited support |
| ERE (POSIX Extended) | egrep, etc | π§ Limited support |
| Python | Python | π§ Limited support |
| RE2 | Go | π§ Limited support |
| Rust | Rust | π§ Limited support |
| Java | Java | π§ Limited support |
| .NET | .NET / C# | π§ Limited support |
| Ruby | Ruby | π§ Limited support |
Warning
Only the RegExp flavor is supported at this time. Other flavors are supported where there is overlap with the RegExp syntax
GitHub supports Mermaid diagrams in readme files, pull requests comments, and code reviews, making this a handy tool to visualise and collaborate on complex regex patterns in your codebase.
The Mermaid docs lists many other integrations, meaning you can share your visualisations where you need. If something isn't natively supported, you can always generate a PNG or SVG image.
Regular expressions are powerful but often hard to read and understand, especially as they grow in complexity. Visualising regex patterns can make them more accessible, helping developers to quickly grasp their structure and logic. This can be particularly useful for:
- π Education - Helping newcomers understand regex concepts visually
- π Documentation - Enhancing technical documentation with clear visual representations
- π€ Collaboration - Facilitating discussions about regex patterns in code reviews or team meetings
- π Debugging - Making it easier to spot errors or inefficiencies in regex patterns
This project uses Bun.
# Clone the repository
git clone https://github.com/tayles/regex-to-mermaid.git
cd regex-to-mermaid
# Install dependencies
bun install
# Run tests
bun test
# Check types
bun run type-check
# Format/lint code
bun run lint| Project | Description |
|---|---|
| Typesafe JavaScript | |
| Fast JavaScript runtime | |
| Diagram generation | |
| Code formatting & linting | |
| Modern regex parsing to AST |

