What the regex explainer does
Regular expressions are dense: ^(?:\+?1[-. ]?)?\(?\d{3}\)? packs a dozen decisions into a few characters. This regex explainer parses the pattern and writes one plain-English line per token, indented so you can see which parts sit inside which group. It is useful when you inherit a pattern from old code, review a pull request or want to confirm that a pattern from Stack Overflow does what its author claimed.
Example explanation
Input:
/^(?<year>\d{4})-(0[1-9]|1[0-2])$/
Output:
^ Assert the start of the string (or of a line with the m flag)
(?<year> Start of capturing group #1 named "year"
\d{4} Match a digit (0-9), exactly 4 times
) End of capturing group #1 named "year"
- Match the character "-"
( Start of capturing group #2
0 Match the character "0"
[1-9] Match one character from the set: "1" to "9"
| OR — alternatively match the expression on the right
1 Match the character "1"
[0-2] Match one character from the set: "0" to "2"
) End of capturing group #2
$ Assert the end of the string (or of a line with the m flag)
The status line adds a summary — here "13 tokens, 2 capturing groups". Group numbers follow the same left-to-right order JavaScript uses, so you know which one is $1 or match[2].
What the explainer understands
- Groups: capturing, named (
(?<name>…)and the Python-style(?P<name>…)), non-capturing(?:…), positive and negative lookahead and lookbehind, and PCRE atomic groups(?>…). - Quantifiers:
*,+,?and{n,m}, each labeled greedy, lazy (*?) or possessive (*+). A quantifier after)is described as repeating the whole group. - Character classes: sets and ranges such as
[a-f0-9], negated sets[^…], POSIX classes like[:alpha:]and escapes inside classes. - Escapes:
\d,\w,\s,\b, hex and Unicode escapes (\x41,é,\u{1F600}), Unicode properties (\p{L}), back-references (\1,\k<name>) and control characters. - PCRE extras:
\A,\Z,\z,\G,\h,\Rand inline modifiers such as(?i), each marked "(PCRE)". - Flags: anything after the closing slash (
g,i,m,s,u,y,d,v, andxfor PCRE) is listed with a short description.
JavaScript vs PCRE patterns
The explainer reads patterns written for PHP, Python or Java as well as JavaScript. After explaining the pattern it also tries to compile it with the browser's RegExp. If that fails, the status line adds "not valid JavaScript syntax (PCRE-only features?)". For example, \Aabc(?i)x++ is explained token by token, but JavaScript would reject it because it has no \A, no inline modifiers and no possessive quantifiers.
Common errors the explainer reports
- Missing ")" — 1 group(s) not closed. Count your parentheses; an escaped
\(does not open a group. - Quantifier "+" at position 0 has nothing to repeat. A quantifier must follow a character, class or group. Escape it as
\+if you meant a literal plus. - Unterminated character class. A
[without a matching], often caused by a literal bracket that should be\[. - Anchor cannot be quantified. Patterns like
^*are almost always a typo.
Once you understand the pattern, try it on real text in the regex tester, or generate matching sample strings with the random data from regex tool to see what it accepts.
Privacy
Parsing happens entirely in your browser. The pattern you paste is never sent to a server, so internal validation rules and proprietary patterns stay on your machine.