Regex Tester

The Complete Regex Guide

How regular expression pattern matching actually works, a full syntax reference, greedy vs lazy matching, and the mistakes that trip up most people.

Open Regex Tester

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

TokenMeaningExample
\dAny digit (0-9)\d{3} matches 555
\wAny word character (letters, digits, underscore)\w+ matches hello_1
\sAny 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 tokengo+ matches goo
*Zero or more of the preceding tokenlo*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 sidecat|dog matches cat or dog
\bA 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

Form ValidationCheck that an email, phone number, or postal code matches an expected shape before submitting a form.
Search & ReplaceFind every instance of a pattern in code or text and replace it, using capture groups to preserve parts of the original.
Log ParsingPull structured fields like timestamps, IP addresses, or status codes out of unstructured log lines.
Input SanitizationStrip or reject characters that shouldn't appear in a username, filename, or URL slug.

Tips & Troubleshooting

Greedy vs Lazy MatchingBy default, + 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.
Forgetting to Escape Special CharactersCharacters like . ( ) [ ] { } + * ? ^ $ | have special meaning. To match them literally - a real period in a domain name, for example - escape them with a backslash: \.
Only the First Match ReturnedWithout the g (global) flag, most methods stop after the first match. Enable g to find every occurrence in the text.
^ and $ Matching Per Line vs Whole StringBy default ^ 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

Why can a pattern that works in Python or PHP fail here, or vice versa?

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.

What is "catastrophic backtracking" and can a pattern freeze my browser?

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.

What's the difference between greedy and lazy matching?

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.

Why does my pattern match more (or less) than I expect?

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.

Does adding parentheses always create a new numbered capture group?

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.

What does the word boundary \b actually match?

\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.

Open Regex Tester