Regex syntax cheat sheet
A regular expression describes a text pattern. The engine scans the test string from left to right and reports every place where the pattern fits. These are the building blocks you will use most often:
| Token | Meaning | Example |
|---|---|---|
. | Any single character except a line break | a.c → abc, a-c |
\d / \D | A digit 0–9 / any non-digit | \d{3} → 123 |
\w / \W | A letter, digit or underscore (ASCII only) / anything else | \w+ → user_01 |
\s | Whitespace: space, tab, line break and other Unicode spaces | \s+ |
\b | Word boundary (zero-width) | \bcat\b does not match “concat” |
[abc] / [^abc] | One of a, b, c / any character except them | [0-9a-f] → a hex digit |
* / + / ? | 0 or more / 1 or more / 0 or 1 | colou?r → color, colour |
{n,m} | Between n and m times | \d{2,4} |
*? / +? | Lazy versions: match as little as possible | <.+?> → one HTML tag |
^ / $ | Start / end of input (of each line with the m flag) | ^# |
( ) / (?: ) | Capturing group / non-capturing group | (\d+)px → $1 = 12 |
(?<name> ) | Named capturing group | (?<year>\d{4}) |
(?= ) / (?<= ) | Lookahead / lookbehind (zero-width) | \d+(?=%) → 50 in “50%” |
a|b | a or b | jpg|png |
Flags
| Flag | Meaning |
|---|---|
g | Global: find all matches (without it, only the first one) |
i | Case-insensitive |
m | Multiline: ^ and $ match at the start and end of every line |
s | dotAll: . also matches line breaks |
u | Unicode mode: treats emoji as one character and enables \p{…} property escapes |
y | Sticky: match only at the current position |
d | Generate start/end indices for each capture group |
Common regex patterns
Copy a pattern into the box above to try it against your own text. Patterns that start with ^ and end with $ check a whole value, such as a form field; remove them to search inside longer text.
| Purpose | Pattern | Matches |
|---|---|---|
| Digits only | ^\d+$ | 12345 |
| Letters and digits only | ^[A-Za-z0-9]+$ | abc123 |
| Email address (simple check) | ^[^\s@]+@[^\s@]+\.[^\s@]+$ | jane.doe@example.com |
| IPv4 address (0–255 each) | ^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$ | 192.168.0.1 |
| Date YYYY-MM-DD | ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$ | 2026-10-04 |
| Time HH:MM (24-hour) | ^([01]\d|2[0-3]):[0-5]\d$ | 23:59 |
| Hex color | ^#(?:[0-9a-fA-F]{3}){1,2}$ | #2563eb |
| 8+ chars with upper, lower and digit | ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$ | Passw0rd |
| Leading / trailing whitespace (flag g) | ^\s+|\s+$ | replace with nothing to trim |
| Repeated word (flags gi) | \b(\w+)\s+\1\b | “the the” |
| HTML tags (flag g) | <[^>]+> | <b>bold</b> |
The password pattern uses three lookaheads ((?=…)): each checks that a character type appears somewhere without consuming any text, so they can all be tested from the start of the string.
Greedy vs. lazy matching
Quantifiers such as * and + are greedy: they match as much as possible and then give back characters only if the rest of the pattern fails. On <b>bold</b>, the pattern <.+> matches the entire string, while the lazy version <.+?> stops at the first > and matches just <b>. A negated class like <[^>]+> gives the same result as the lazy version and is usually faster.
Using the replacement field
The replacement is applied with JavaScript's String.prototype.replace. Use $1, $2… for numbered groups, $<name> for named groups, $& for the whole match and $$ for a literal dollar sign. For example, the ISO date preset with the replacement $<month>/$<day>/$<year> turns 2026-10-04 into 10/04/2026. Without the g flag only the first match is replaced.
Frequently asked questions
Will my regex behave the same in Python, PHP or Java?
The core syntax is shared, but details differ between engines. This tester uses your browser's JavaScript engine. For example, Python writes named groups as (?P<name>…), and support for lookbehind or Unicode properties varies by language and version. Test in your target language before shipping.
Why did the page freeze when I typed a pattern?
Nested quantifiers such as (a+)+$ can cause catastrophic backtracking, where the work grows exponentially with the input length (ReDoS). Reload the page and rewrite the pattern so that repetitions cannot overlap.
What is the perfect regex for email addresses?
A regex that fully follows RFC 5322 is extremely long and impractical. Use a simple pattern like the preset to catch typos, then confirm the address really exists by sending a verification email.
Is my test data uploaded?
No. Matching and replacing happen entirely in your browser, so it is safe to paste log files or other internal text.