Regex Cheat Sheet
A searchable reference of common regular expression syntax — character classes, anchors, quantifiers, groups, lookaround, and flags — with examples.
| Category | Pattern | Description | Example |
|---|---|---|---|
Character classes | . | Any character except newline | a.c matches abc |
Character classes | \d | Digit (0-9) | \d+ matches 123 |
Character classes | \D | Non-digit | \D+ matches abc |
Character classes | \w | Word character (letters, digits, underscore) | \w+ matches hello_1 |
Character classes | \W | Non-word character | \W matches @ |
Character classes | \s | Whitespace | \s+ matches spaces/tabs |
Character classes | \S | Non-whitespace | \S+ matches non-space run |
Character classes | [abc] | Any of a, b, or c | [abc] matches a, b, or c |
Character classes | [^abc] | Anything except a, b, or c | [^abc] matches d, e, ... |
Character classes | [a-z] | Any lowercase letter (range) | [a-z]+ matches hello |
Anchors | ^ | Start of string (or line with m flag) | ^abc matches abc at start |
Anchors | $ | End of string (or line with m flag) | abc$ matches abc at end |
Anchors | \b | Word boundary | \bcat\b matches whole word cat |
Anchors | \B | Non-word boundary | \Bcat matches concatenate |
Quantifiers | * | 0 or more | ab*c matches ac, abc, abbc |
Quantifiers | + | 1 or more | ab+c matches abc, abbc |
Quantifiers | ? | 0 or 1 (optional) | ab?c matches ac or abc |
Quantifiers | {n} | Exactly n times | a{3} matches aaa |
Quantifiers | {n,} | n or more times | a{2,} matches aa, aaa, ... |
Quantifiers | {n,m} | Between n and m times | a{2,4} matches aa to aaaa |
Quantifiers | *? | 0 or more, lazy (non-greedy) | <.+?> matches shortest tag |
Groups | (abc) | Capturing group | (ab)+ captures repeated ab |
Groups | (?:abc) | Non-capturing group | (?:ab)+ groups without capturing |
Groups | (?<name>abc) | Named capturing group | (?<year>\d{4}) |
Groups | a|b | Alternation (OR) | cat|dog matches cat or dog |
Lookaround | (?=abc) | Positive lookahead | \d+(?=px) matches digits before px |
Lookaround | (?!abc) | Negative lookahead | \d+(?!px) digits not before px |
Lookaround | (?<=abc) | Positive lookbehind | (?<=\$)\d+ digits after $ |
Lookaround | (?<!abc) | Negative lookbehind | (?<!\$)\d+ digits not after $ |
Flags | g | Global — find all matches | /abc/g |
Flags | i | Case-insensitive | /abc/i matches ABC |
Flags | m | Multiline — ^ and $ match line boundaries | /^abc/m |
Flags | s | Dot matches newline too | /a.b/s |
Flags | u | Unicode mode | /\u{1F600}/u |