Regular Expression Explainer
Explain a JavaScript regex token by token and test it against sample text.
Written by Golam Rabbani, Founder & Lead Engineer
How to use this regular expression explainer
- Paste a regex pattern (either bare like ^\d{3}-\d{4}$ or a JS literal like /foo|bar/gi).
- Enter the flags you want (g, i, m, s, u, y, d) — or leave them in if you used a /…/flags literal.
- Optionally paste sample text to test the regex against — the tool will list every match.
- Press Explain to compile the regex with the real JS engine and break it down token by token.
- Use Copy explanation to grab a plain-text version of the breakdown for code reviews or documentation.
About this regular expression explainer
The regular expression explainer parses your actual pattern with a hand-written tokenizer, then describes each construct in plain English: anchors, character classes, groups, lookaround, escapes, quantifiers (greedy and lazy), backreferences, named captures, and Unicode escapes. It also compiles the regex with the real JavaScript engine via `new RegExp(...)`, so any syntax error you get matches what your code would throw at runtime. If you provide sample text, it runs the regex against it and lists every match with its index.
For example, feeding in the pattern "^(\d{3})-(\d{4})$" with flag "g" produces a token list: "^" — start of string; "(" — start of capturing group; "\d" — any digit; "{3}" — quantifier (exactly 3 times); ")" — end of group; "-" — literal "-"; "(" — start of group; "\d{4}" — any digit, 4 times; ")" — end of group; "$" — end of string. With sample text "Call 555-1234 or 800-5555-1212" and the global flag, the matches panel shows "@5: 555-1234" (the 800 number has too many digits to match in full). This is genuinely useful for grokking somebody else's regex, debugging your own, and teaching the syntax to teammates. All parsing and matching is local — nothing is uploaded.
FAQ
- Does this actually parse my regex, or is it a canned explanation?
- It actually parses it. A hand-written tokenizer walks your real pattern character by character and labels every construct. Compilation goes through the JS engine via new RegExp, so syntax errors are the real errors you would hit in your code.
- Which flags are explained?
- All seven JavaScript flags: g (global), i (case-insensitive), m (multiline), s (dotAll), u (unicode), y (sticky), and d (hasIndices). Each appears in the result with a one-line description.
- Can I paste a /pattern/flags literal?
- Yes. Anything starting with "/" and ending with "/flags" is treated as a JS regex literal — the pattern is extracted and the flags field is overridden by what you wrote between the closing slash and the end of the literal.
- What about lookbehind and named groups?
- Both are supported. (?<=…) and (?<!…) are recognised as positive/negative lookbehind, and (?<name>…) is recognised as a named capturing group with its name extracted.
- Is my regex sent anywhere?
- No. Parsing, compilation, and any sample-text matching all happen in your browser. Nothing is sent to a server.
- Does the sample-text runner support catastrophic backtracking patterns?
- It runs the same engine as your browser, so very pathological patterns (e.g. nested quantifiers on overlapping sets) can still hang. A safety counter caps matches at 1,000 hits and the run is bounded by the engine itself.