JavaScript regular expressions
Regex Explainer
Read a JavaScript pattern token by token, spot common traps, and inspect every capture.
Syntax diagnostics
Token breakdown
Trap warnings
Test results
JavaScript regex guide
Paste a JavaScript regular expression, add optional flags, and get a token breakdown, position-based diagnostics, common trap warnings, and JavaScript test results in your browser.
How to use it
- Paste a pattern into Regex pattern, optionally enter one or more supported flags in Flags, and add test text.
- Select Explain regex.
- Read the syntax diagnostics, token breakdown, trap warnings, and match spans with captured values.
The page starts with a date-validation example containing named capturing groups, character classes, quantifiers, and a positive lookahead. Its test text is 2024-06-15.
What this Regex Explainer shows
Token-by-token JavaScript regex explanation
The tokenizer lists each recognized fragment with its original text, zero-based position, category, and a plain-English explanation. It recognizes literals, character classes, quantifiers, groups, anchors, alternation, escapes, lookaround, and backreferences. A named group such as (?<year>...) is identified with its group number. Numbered references such as \1 and named references such as \k<year> are described as references to captured text.
Syntax diagnostics include a position for issues such as an unclosed group or character class, a quantifier with no target, a trailing backslash, an invalid Unicode property escape, and an unsupported or repeated flag. PCRE-only forms such as atomic groups (?>...), recursion (?R), possessive quantifiers, and \A or \z are called out as not valid in JavaScript.
Greedy and lazy quantifiers
Quantifiers set a repetition range for the preceding element:
*:0 ≤ repetitions ≤ ∞+:1 ≤ repetitions ≤ ∞?:0 ≤ repetitions ≤ 1{n}:n ≤ repetitions ≤ n{n,}:n ≤ repetitions ≤ ∞{n,m}:n ≤ repetitions ≤ m
A greedy quantifier tries the larger allowed count first. Add a trailing ? to make it lazy, so it tries the smaller count first. The tool explains both the range and whether the recognized quantifier is greedy or lazy.
Lookahead and lookbehind
Lookahead checks text after the current position without consuming it. Positive lookahead uses (?=...); negative lookahead uses (?!...). Positive and negative lookbehind use (?<=...) and (?<!...) to check text before the current position. Lookbehind became part of JavaScript in ES2018; browser support still depends on the browser you target.
Trap warnings
The tool reports selected, independent heuristics rather than declaring that a pattern is safe:
- Nested repetition: structures such as
(a+)+can cause potential catastrophic backtracking and ReDoS risk. A backtracking engine may retry many ways to split the same input. Avoid nested repetition, or use a more specific or bounded pattern. - Possible domain dot: a bare dot between domain-like character runs, as in
example.com, matches any character except line terminators. If a literal separator is intended, use\.. - Empty alternation branch:
(a|)and similar forms contain a branch that matches the empty string. Confirm that the optional behavior is intentional. - Lazy quantifier before
$: in a pattern such as.*?$, the lazy choice cannot improve the final match when the end anchor still requires the input end. Consider an explicit character class when that matches your intent.
These warnings are structural hints, not a complete ReDoS analysis or a security guarantee. Browser-based regex execution can still be expensive for a dangerous pattern.
Character class quick reference
In JavaScript patterns, \d is a digit character escape, \w is a word-character escape, and \s is a whitespace escape. Their uppercase forms match the corresponding non-members. Character classes such as [0-9] are shown as one token, including the complete bracketed fragment. Unicode property escapes such as \p{L} are recognized only when the u flag is present.
JavaScript flags
This page accepts the supported flag set gimsuy:
g— global matching; the test view lists all matches.i— case-insensitive matching.m— multiline behavior for^and$.s— dotAll behavior for..u— Unicode mode and Unicode property escapes.y— sticky matching according to JavaScript regular-expression behavior.
Captures and test results
Capturing groups are numbered from 1 by the order of their opening parentheses. Non-capturing groups and the lookaround wrapper do not receive a number, while capturing groups inside lookaround still do. Test results show each match, its half-open text span such as [0, 10), and each captured group by number. Named captures are labeled when available. A group that did not participate is shown as undefined; an empty match is explicitly labeled. If there is no result, the page says No match.
The tokenizer runs in the browser, and valid patterns are tested with the browser’s JavaScript regular-expression engine. PCRE syntax is not converted into JavaScript syntax.
Frequently asked questions
What is a regex explainer?
A regex explainer breaks a pattern into recognizable tokens and translates each token into plain English. This page also gives token positions, selected syntax diagnostics, trap warnings, and test match information for JavaScript patterns.
How do I read a JavaScript regular expression?
Start with anchors such as ^ and $, then inspect groups, character classes, escapes, and quantifiers. Next check alternation and backreferences. The token table presents those pieces in source order with their positions and explanations.
What is the difference between greedy and lazy quantifiers?
Greedy quantifiers try the larger repetition count first. Lazy quantifiers, written with a trailing ?, try the smaller count first. For example, + is greedy and +? is lazy; both require at least one repetition.
Does JavaScript regex support lookbehind?
Yes. Modern JavaScript supports positive and negative lookbehind, which entered the language in ES2018. This tool explains (?<=...) and (?<!...) as JavaScript lookaround syntax. Check your target browsers for compatibility.
What do \d, \w, and \s mean?
They are JavaScript character escapes for digits, word characters, and whitespace. The uppercase forms represent the corresponding non-digit, non-word, and non-whitespace sets. The tool identifies these escapes in the token breakdown.
What is catastrophic backtracking, and how is it related to ReDoS?
Some nested or overlapping repetitions make a backtracking engine retry many possible input splits, especially when a later part fails. That rapid growth in attempts is called catastrophic backtracking and can contribute to a Regular Expression Denial of Service, or ReDoS. This page flags a recognizable nested-repetition pattern; it is not a complete security proof.
How are capture groups numbered?
Capturing groups are numbered from 1 from left to right by opening parenthesis. Non-capturing groups and lookaround wrappers do not take a number, but captures inside lookaround do. Named groups also have a number and a name.
Can this explain PCRE syntax?
No. It uses the JavaScript regex boundary. Atomic groups (?>...), possessive quantifiers, recursion such as (?R), and the PCRE anchors \A and \z are reported as PCRE-only and not valid in JavaScript.