How Regex Pattern Matching Works
A regular expression engine reads your pattern and your text at the same time, trying to line up the pattern's tokens against a run of characters starting at each position in the text. When every token in the pattern is satisfied in order, that stretch of text becomes a match. If a token fails partway through, the engine backtracks - it gives up ground it already matched and tries a different way of satisfying an earlier, flexible token (like a quantifier) before moving on.
This tool runs that process live, using JavaScript's built-in RegExp engine directly in your browser, and highlights every match plus its capture groups as you type - so you can see exactly what the engine chose to match, not just whether it matched.
Regex Syntax Reference
| Token | Meaning | Example |
|---|---|---|
\d | Any digit (0-9) | \d{3} matches 555 |
\w | Any word character (letters, digits, underscore) | \w+ matches hello_1 |
\s | Any whitespace character (space, tab, newline) | a\sb matches a b |
. | Any character except a newline (unless the s flag is set) | c.t matches cat, cut |
+ | One or more of the preceding token | go+ matches goo |
* | Zero or more of the preceding token | lo*l matches ll, lool |
? | Zero or one of the preceding token (optional) | colou?r matches color, colour |
{n,m} | Between n and m repetitions of the preceding token | \d{2,4} matches 2-4 digits |
^ | Start of the string (or line, with the m flag) | ^Hello matches text starting with Hello |
$ | End of the string (or line, with the m flag) | end$ matches text ending in end |
[abc] | A character class - any one character from the set | [aeiou] matches any vowel |
[^abc] | A negated character class - any character not in the set | [^0-9] matches any non-digit |
(...) | A capturing group - remembers what it matched | (\d{4})-(\d{2}) captures year and month |
(?<name>...) | A named capturing group | (?<year>\d{4}) |
(?:...) | A non-capturing group - groups without remembering | (?:abc)+ |
| | Alternation - matches either side | cat|dog matches cat or dog |
\b | A word boundary (zero-width, matches nothing itself) | \bcat\b won't match "category" |
(?=...) | Positive lookahead - matches only if followed by the pattern | \d+(?=px) matches digits before "px" |
(?!...) | Negative lookahead - matches only if NOT followed by the pattern | \d+(?!px) |
Common Regex Use Cases
Tips & Troubleshooting
+ and * are greedy - they grab as much text as possible. A pattern like <.+> against <b>bold</b> matches the whole string, not just <b>. Add a ? to make it lazy: <.+?> stops at the first > instead.. ( ) [ ] { } + * ? ^ $ | have special meaning. To match them literally - a real period in a domain name, for example - escape them with a backslash: \.g (global) flag, most methods stop after the first match. Enable g to find every occurrence in the text.^ and $ anchor to the start/end of the entire string. Add the m (multiline) flag to anchor them to the start/end of each line instead.Frequently Asked Questions
Different regex engines ("flavors") implement overlapping but not identical feature sets. JavaScript's engine, for example, has its own syntax for named groups and Unicode property escapes, historically had more limited lookbehind support than PCRE, and doesn't support possessive quantifiers at all. A pattern tested here reflects exactly what a browser or Node.js runtime will do, but may need small adjustments to run unmodified in Python's re module, PHP's PCRE, or another language's engine.
Certain patterns with nested or overlapping quantifiers - like (a+)+b tested against a long run of a characters with no trailing b - force the engine to try an exponential number of ways to backtrack before it can give up. That can freeze a browser tab or script for seconds or minutes on a single call, a problem known as catastrophic backtracking or ReDoS (regular expression denial of service). It's worth testing any pattern with nested quantifiers against a deliberately non-matching, repetitive input to catch this before it ships.
A greedy quantifier like + or * matches as much text as possible before backtracking. Adding a ? after it (e.g. +? or *?) makes it lazy, matching as little text as possible - important when a pattern like <.+> ends up spanning multiple tags instead of matching just one.
The most common causes are an unescaped special character (like a literal . or ( that should be \. or \(), a greedy quantifier grabbing more than intended, or forgetting the g flag so only the first match is returned.
No. Plain parentheses (...) do create a numbered capturing group, but (?:...) is a non-capturing group - it still lets you apply a quantifier or alternation to several tokens as a unit, without adding an extra numbered group you'd otherwise have to keep track of. Reaching for (?:...) whenever you don't actually need that group's matched text keeps the numbering of your real capture groups simpler.
\b matches a zero-width position between a word character (\w) and a non-word character (or the start/end of the string) - it doesn't consume any characters itself, it just anchors the match to a word edge.