DevTools Logo

Interactive RegEx Railroad & DFA/NFA Stepper

Examples

The classic catastrophic pattern

Input
(a+)+$   against   aaaaaaaaaaaaX
Output
ReDoS: critical — thousands of backtracks on 13 characters, step ratio ≫ 8/char

The nested quantifier lets the engine retry exponentially many partitions of the a's; the debugger makes the explosion visible and safely bounded.

Greedy vs lazy in slow motion

Input
<.+>   vs   <.+?>   against   <a><b>
Output
greedy: matches <a><b> after unwinding · lazy: matches <a> immediately

Step both and watch the greedy engine run to the end of the line and backtrack, while the lazy variant exits at the first '>'.

Simplify a verbose pattern

Input
(?:[0-9\d]{1,})-(?:[A-Za-z]){4,}
Output
\d+-[A-Za-z]{4,}

Redundant classes collapse to shorthands, {1,} becomes +, and non-capturing groups that carry no precedence are dropped — language-identical, verified against the native engine.

Lookbehind as a diagram

Input
(?<=@)\w+
Output
dashed "(?<= … )" frame wrapping the \w+ lane

Zero-width constructs get their own visual vocabulary so the diagram stays honest about what consumes input and what only peeks.

About this tool

The Interactive RegEx Railroad & DFA/NFA Stepper shows what a regular expression actually is: a machine. Type a JavaScript pattern and it is parsed into an explicit syntax tree — no black box — then rendered three ways: as a classic railroad (syntax) diagram you can export as vector SVG, as the NFA produced by Thompson's construction, and as the DFA derived from that NFA by subset construction, with ε-edges drawn dashed and accepting states ringed.

The step debugger is where intuition gets built. A faithful reimplementation of a backtracking regex engine runs your pattern against your test text and logs every instruction: characters consumed, alternatives pushed, assertions checked, and — the part browsers never show you — every unwind. Play it, pause it, scrub it, or step one instruction at a time while the input caret, live backtrack counter and engine log update together.

Because backtracking is also a security story, the tool grades every run for ReDoS risk: a static analysis flags nested quantifiers with overlapping starts like (a+)+ and ambiguous alternation like (a|a)*, while a dynamic signal measures steps executed per input character. The simulation is hard-budgeted, so a catastrophic pattern truncates with a warning instead of freezing the tab — and the simplified-pattern export (a{1,} → a+, [0-9\d] → \d, redundant groups removed) gives you the safe rewrite to copy.

Everything is computed locally: the parser, compiler, automata and VM are pure TypeScript with no RegExp delegation and no network calls, so patterns and test text never leave the page.

How to use this tool

  1. Enter pattern and text

    Type a JavaScript regex (without delimiters), toggle the i/m/s flags, and add the text to debug against — or pick a preset like the ReDoS demo.

  2. Read the railroad diagram

    Boxes are terminals, stacked rows are alternation, the lane under a box is its quantifier loop, and dashed frames are lookaround.

  3. Compare NFA and DFA

    Switch to the NFA · DFA tab: the NFA mirrors Thompson's construction exactly; the DFA shows the subset-construction view with merged states and interval-labelled edges.

  4. Step the engine

    Open the step debugger and play, pause, scrub or single-step. The log lists pc (instruction), pos (text index) and the action; the counter accumulates backtracks live.

  5. Check ReDoS and export

    Read the risk badge and its reasons, then export the standalone SVG diagram and the optimized pattern from the export panel.

Use cases

Debugging a pattern that 'works on regex101 but not in prod'

The step log shows exactly which alternative the engine takes and where it unwinds, on the exact text that fails.

ReDoS review before shipping user-supplied patterns

Paste candidate patterns, read the risk grade and the structural reason, and replace with the simplified export.

Teaching and learning automata

(a|b)*abb is the canonical Thompson example — draw it, derive the DFA, then watch the backtracking engine disagree with both.

Documenting a gnarly pattern for teammates

Export the railroad SVG straight into a PR description or README instead of describing the regex in prose.

Common mistakes

Mistake:Reading the DFA as the execution model

Fix:Browsers run backtracking, not DFAs — the NFA/DFA views explain the language, the step debugger shows the real engine.

Mistake:Trusting a 'safe' score on short test text

Fix:ReDoS scales with input length; the static reasons (nested quantifiers, ambiguous alternation) are the ones that matter regardless of sample size.

Mistake:Expecting every flavor to render

Fix:The parser targets the JavaScript flavor's core; \p{...} property escapes are reported as unsupported rather than drawn wrong.

Frequently asked questions

How is this different from the Regex Tester?

The Regex Tester answers 'what matches?'; this tool answers 'why, and how fast?'. It parses your pattern into an explicit tree and renders it as a railroad diagram, an NFA (Thompson's construction) and a DFA (subset construction), then runs a faithful reimplementation of a backtracking engine one instruction at a time so you can watch branches, assertions and unwinds happen. Use the Tester for everyday matching and substitution; step over here when you need to understand or de-risk a pattern.

Does the step debugger run my browser's real regex engine?

No — browsers don't expose instruction-level tracing, so the tool re-implements a backtracking VM in pure TypeScript. It is verified against the native engine: for every supported construct (classes, groups, backreferences, lookaround, lazy quantifiers, i/m/s flags) it must report the same first match on a fixed corpus, which is enforced by the test suite. The upside of the re-implementation is that it is hard-budgeted: a catastrophic pattern truncates with a warning instead of freezing the tab.

How is the ReDoS score computed?

Two signals are combined. Statically, the AST is scanned for nested quantifiers with overlapping first characters — (a+)+ — and repeated alternation with ambiguous branches — (a|a)* — the two classic exponential shapes. Dynamically, the simulator counts executed instructions per input character: single digits per char is linear and Low, roughly 2–8 per char suggests quadratic behavior (Medium), higher ratios or an exhausted step budget are Critical. The reasons are always listed with the badge.

What does the optimized pattern export change?

Only things that keep the language identical, verified against the native engine: verbose quantifiers become their shorthand (a{1,} → a+, a{0,1} → a?), character classes are deduped and merged ([0-9\d] → \d, [cab] → [a-c]), exact duplicate alternation branches are dropped, and non-capturing groups that carry no precedence are removed. If a rewrite could change semantics, the original is kept.

Why does the DFA sometimes cap out?

Subset construction can blow up exponentially on ambiguous patterns — the display stops at 400 states and warns you. That explosion is itself information: ambiguity in the DFA is the same ambiguity that causes catastrophic backtracking, so treat the warning as a design smell and check the ReDoS panel.

Which regex syntax is supported?

The JavaScript (ECMAScript) core: literals and escapes (\x41, \u{1F600}, control escapes), character classes with ranges and negation, the perl shorthands \d \w \s, capturing/named/non-capturing groups, alternation, greedy and lazy quantifiers including {m,n}, anchors ^ $ \b \B, numbered and named backreferences, and lookahead/lookbehind (positive and negative). Unicode property escapes (\p{...}) and the v flag are not drawn — the parser reports them as unsupported instead of rendering something misleading.

Is my pattern or test text sent anywhere?

No. Parsing, compilation, automata construction and simulation are all pure client-side TypeScript with no network calls. The pattern is encoded in the page URL only so you can share the exact view; nothing is transmitted or stored on a server.

References & standards

.env Secret Safety Linter

Validate .env syntax and spot likely leaked credentials or placeholder secrets.

.htaccess Generator

Build an Apache .htaccess — HTTPS redirects, rewrites, error pages, security headers, and basic auth

2D Fluid Dynamics & Particle Physics Sandbox

A physics playground with two engines sharing one canvas. The fluid mode runs a real Eulerian Navier-Stokes solver — semi-Lagrangian advection, explicit diffusion, Jacobi pressure projection and vorticity confinement — that you paint with dye and velocity using mouse or touch. The particle mode is a damped Verlet N-body engine with gravity wells, Coulomb attractors and repulsors, spring links, wall restitution and elastic circle collisions. Particle counts adapt to the device (100k target on WebGL2 desktop, 2k-5k on CPU fallback), and every configuration exports as runnable code: a Matter.js world, a PixiJS ticker setup, or a standalone vanilla Canvas engine.

AI Function Calling Schema Generator

Generate function calling JSON schemas from TypeScript interfaces

Algorithm Arena & Data Structure Visualizer

Interactive arena for algorithms and data structures: eight sorting algorithms racing on one seeded input, six pathfinding algorithms walking generated mazes, and BST, AVL, Red-Black, B-tree, heap and trie operations replayed step by step with rotations highlighted. Every run shows the Big-O table and the phase-highlighted reference implementation in four languages, and compared values can be played as pitch (sonification) through the shared audio engine. Everything runs client-side; inputs never leave the browser.

Ansible Playbook Generator

Build an Ansible playbook — hosts, vars, and tasks with modules — as YAML