The building blocks
| Symbol | Matches | Example |
|---|---|---|
. | Any character except a line break | c.t → cat, cut |
\d | A digit 0–9 | \d\d → 42 |
\w | A letter, digit or underscore | \w+ → hello_1 |
\s | Whitespace | a\sb → a b |
[abc] | Any one of the listed characters | gr[ae]y → grey, gray |
[^0-9] | Any character not listed | a non-digit |
Quantifiers, anchors and groups
*zero or more,+one or more,?optional,{3}exactly three,{2,4}two to four.^start and$end of the text (or of each line with themflag).\bis a word boundary.( )groups part of a pattern and captures it;(?<year>\d{4})creates a named group.a|bmeans a or b.- To match a special character literally, escape it:
\.matches a full stop.
Flags
g— find all matches, not just the first.i— ignore case.m— make^and$match at every line.s— let.match line breaks too.u— full Unicode support.
Practical patterns
| Task | Pattern |
|---|---|
| Simple email check | ^[^\s@]+@[^\s@]+\.[^\s@]+$ |
| ISO date (2026-11-06) | \b\d{4}-\d{2}-\d{2}\b |
| Whole numbers with optional decimals | -?\d+(\.\d+)? |
| Repeated word (“the the”) | \b(\w+)\s+\1\b |
| Hashtags | #\w+ |
Real-world formats are messy — fully validating email addresses with a regex is famously hard — so treat these as practical filters, not perfect validators.
Test before you use
- Type a pattern in the Regex Tester, without the surrounding slashes.
- Tick the flags you need. Without
g, only the first match is found. - Paste test text with examples that should and should not match. Matches are highlighted as you type.
- Check the table for each match’s position and capture groups.
- Enter a replacement to preview the result, using
$1,$<name>or$&.
Patterns run in a separate worker and stop after 2 seconds, so a runaway pattern cannot freeze the page. When the pattern works, apply it to a whole document with the Find and Replace Tool.
Frequently asked questions
Does regex work the same in every language?
The basics are shared, but details differ. The tester uses JavaScript syntax, which is close to, but not identical to, PCRE (PHP) and Python.
What is catastrophic backtracking?
Patterns with nested quantifiers, such as (a+)+, can take exponentially long on some inputs. Keep quantifiers simple and specific to avoid it.