Regex Cheat Sheet
Regular expressions describe patterns in text. The syntax divides into character classes (what to match), quantifiers (how many), anchors (where), groups (what to capture) and flags (how the whole pattern behaves). This reference uses JavaScript syntax, which is what runs in a browser and is close to PCRE for everyday patterns.
Almost all practical regex work uses a small subset of the syntax. The tables below are grouped by intent rather than alphabetically, because you generally know what you are trying to express and need the notation for it.
One warning worth reading before the tables: nesting a quantifier inside another quantified group, as in (a+)+, can make matching take exponential time. On a few dozen characters that is minutes of pegged CPU. It is the single most dangerous thing in this reference.
Character classes
11 entriesWhat a single position may match.
| Pattern | Matches | Example |
|---|---|---|
| . | Any character except a newline | a.c matches abc, a-c |
| \d | A digit, 0–9 | \d\d matches 42 |
| \D | Anything that is not a digit | \D+ matches abc |
| \w | Word character: letter, digit or underscore | \w+ matches user_1 |
| \W | Anything that is not a word character | \W matches a space or ! |
| \s | Whitespace: space, tab, newline | \s+ collapses runs of space |
| \S | Anything that is not whitespace | \S+ matches one token |
| [abc] | Any one of a, b or c | [aeiou] matches a vowel |
| [^abc] | Any character except a, b or c | [^0-9] matches a non-digit |
| [a-z] | A range | [A-Za-z0-9] matches alphanumerics |
| \. | A literal dot | example\.com |
Quantifiers
8 entriesHow many times the preceding item may repeat.
| Pattern | Matches | Example |
|---|---|---|
| * | Zero or more | ab*c matches ac, abc, abbc |
| + | One or more | ab+c matches abc but not ac |
| ? | Zero or one — optional | colou?r matches both spellings |
| {3} | Exactly three | \d{4} matches a year |
| {2,} | Two or more | \d{2,} matches 42 and 4242 |
| {2,4} | Between two and four | \w{2,4} |
| *? | Lazy: as few as possible | \[(.*?)\] stops at the first ] |
| +? | Lazy one or more | <.+?> matches one tag, not the whole line |
Anchors and boundaries
4 entriesWhere in the text a match may begin or end.
| Pattern | Matches | Example |
|---|---|---|
| ^ | Start of string, or of a line with the m flag | ^Error matches a line starting with Error |
| $ | End of string, or of a line with the m flag | \.$ matches a trailing full stop |
| \b | A word boundary | \bcat\b matches cat but not category |
| \B | Not a word boundary | \Bcat matches the cat inside bobcat |
Groups and alternation
6 entriesCapturing parts of a match, and matching alternatives.
| Pattern | Meaning | Example |
|---|---|---|
| (abc) | Capturing group, numbered from 1 | (\d{4})-(\d{2}) captures year and month |
| (?:abc) | Non-capturing group | Use when you only need a quantifier |
| (?<name>abc) | Named capturing group | Referenced as groups.name |
| a|b | Either a or b | cat|dog matches either word |
| \1 | Backreference to group 1 | (\w)\1 matches a doubled letter |
| $1 | Group 1 in a replacement | Swap with $2 $1 |
Lookaround
4 entriesAssert what is next to a match without consuming it.
| Pattern | Meaning | Example |
|---|---|---|
| (?=abc) | Positive lookahead — followed by | \d+(?= items) matches the number only |
| (?!abc) | Negative lookahead — not followed by | foo(?!bar) matches foo unless bar follows |
| (?<=abc) | Positive lookbehind — preceded by | (?<=\$)\d+ matches the amount after $ |
| (?<!abc) | Negative lookbehind — not preceded by | (?<!\$)\d+ |
Flags
6 entriesApplied to the whole pattern.
| Flag | Meaning | Notes |
|---|---|---|
| g | Global — find every match | Without it, matching stops at the first result |
| i | Case-insensitive | Applies to the whole pattern |
| m | Multiline | Makes ^ and $ match at line boundaries |
| s | Dot matches newlines | Sometimes called dotall |
| u | Unicode mode | Required for \u{…} and \p{…} escapes |
| y | Sticky | Matches only from lastIndex |
Notes
- This is JavaScript regex syntax, which is what runs in a browser. It is close to PCRE for everyday patterns but has no atomic groups and no possessive quantifiers, and lookbehind support depends on the engine version.
- Avoid nesting quantifiers. A pattern like (a+)+$ can force exponential backtracking, taking minutes on a few dozen characters. Rewrite so each position can only be matched one way.
- Do not parse HTML with a regex. Nesting is not a regular language, so a pattern that works on your sample will break on nested or malformed markup. Extracting one attribute from known-shape markup is fine.
Frequently asked questions
What is the difference between greedy and lazy?
When should I use a non-capturing group?
Why does my pattern hang the browser?
Does \d match non-Latin digits?
Related tools
All tools →- Regex Tester Test a regular expression and see every match highlighted. In your browser
- Find and Replace Replace text everywhere, with optional regex support. In your browser
- Email Extractor Pull every email address out of a block of text. In your browser
- URL Extractor Find every link in text or HTML. In your browser
- Text Difference Checker Compare two versions of a text and see what changed. In your browser
More reference tables
All references →Last reviewed .