Regex Cheat Sheet
Complete reference guide for regular expression syntax with examples
AnchorsCharacter ClassesQuantifiersGroups & CapturingLookahead & LookbehindFlags / ModifiersSpecial Characters & EscapesSubstitution / Replacement
Anchors
Character Classes
| Syntax | Description | Example | Try |
|---|---|---|---|
[abc] | Match any character in brackets | [aeiou] matches vowels | Try it → |
[^abc] | Match any character NOT in brackets | [^0-9] matches non-digits | Try it → |
[a-z] | Match any character in range | [a-zA-Z] matches letters | Try it → |
. | Any character except newline | a.c matches 'abc', 'a1c', etc. | Try it → |
\w | Word character [a-zA-Z0-9_] | \w+ matches 'hello_123' | Try it → |
\W | Non-word character | \W matches '@', ' ', etc. | Try it → |
\d | Digit [0-9] | \d{3} matches '123' | Try it → |
\D | Non-digit | \D+ matches 'abc' | Try it → |
\s | Whitespace (space, tab, newline) | \s+ matches spaces | Try it → |
\S | Non-whitespace | \S+ matches 'word' | Try it → |
Quantifiers
| Syntax | Description | Example | Try |
|---|---|---|---|
* | 0 or more (greedy) | a* matches '', 'a', 'aaa' | Try it → |
+ | 1 or more (greedy) | a+ matches 'a', 'aaa' but not '' | Try it → |
? | 0 or 1 (optional) | colou?r matches 'color' and 'colour' | Try it → |
{n} | Exactly n times | \d{4} matches '2024' | Try it → |
{n,} | n or more times | \d{2,} matches '12', '123', '1234' | Try it → |
{n,m} | Between n and m times | \d{2,4} matches '12', '123', '1234' | Try it → |
*? | 0 or more (lazy) | a*? matches minimal 'a's | Try it → |
+? | 1 or more (lazy) | <.+?> matches single HTML tag | Try it → |
?? | 0 or 1 (lazy) | Matches minimal optional | Try it → |
Groups & Capturing
| Syntax | Description | Example | Try |
|---|---|---|---|
(abc) | Capturing group | (\d+) captures digits | Try it → |
(?:abc) | Non-capturing group | (?:Mr|Ms)\.? matches title | Try it → |
(?<name>abc) | Named capturing group | (?<year>\d{4}) captures as 'year' | Try it → |
\1 | Backreference to group 1 | (\w)\1 matches 'aa', 'bb' | Try it → |
\k<name> | Backreference to named group | (?<char>\w)\k<char> | Try it → |
(a|b) | Alternation (OR) | (cat|dog) matches 'cat' or 'dog' | Try it → |
Lookahead & Lookbehind
| Syntax | Description | Example | Try |
|---|---|---|---|
(?=abc) | Positive lookahead | \d(?=px) matches digit before 'px' | Try it → |
(?!abc) | Negative lookahead | \d(?!px) matches digit NOT before 'px' | Try it → |
(?<=abc) | Positive lookbehind | (?<=\$)\d+ matches digits after '$' | Try it → |
(?<!abc) | Negative lookbehind | (?<!\$)\d+ matches digits NOT after '$' | Try it → |
Flags / Modifiers
| Syntax | Description | Example | Try |
|---|---|---|---|
g | Global - find all matches | /a/g finds all 'a' in 'banana' | Try it → |
i | Case-insensitive | /hello/i matches 'HELLO' | Try it → |
m | Multiline - ^ and $ match line boundaries | /^start/m | Try it → |
s | Dotall - . matches newlines | /a.b/s matches 'a\nb' | Try it → |
u | Unicode support | /\p{L}/u matches Unicode letters | Try it → |
y | Sticky - match at lastIndex only | For iterative matching | Try it → |
Special Characters & Escapes
| Syntax | Description | Example | Try |
|---|---|---|---|
\ | Escape special character | \. matches literal dot | Try it → |
\n | Newline | line1\nline2 | Try it → |
\r | Carriage return | Windows line ending \r\n | Try it → |
\t | Tab | column1\tcolumn2 | Try it → |
\0 | Null character | String terminator | Try it → |
\xhh | Hex character | \x41 matches 'A' | Try it → |
\uhhhh | Unicode character | \u0041 matches 'A' | Try it → |
Substitution / Replacement
| Syntax | Description | Example | Try |
|---|---|---|---|
$1, $2 | Insert captured group | '(\w+) (\w+)' → '$2 $1' | Try it → |
$& | Insert entire match | '\d+' → '[$&]' wraps numbers | Try it → |
$` | Insert text before match | Before the match | Try it → |
$' | Insert text after match | After the match | Try it → |
$$ | Insert literal $ | 'price' → '$$99' | Try it → |
$<name> | Insert named group | '(?<first>\w+)' → '$<first>' | Try it → |