RE

Regex Cheat Sheet

Regular expression reference with patterns, quantifiers, lookaheads, and common recipes. Copy-ready examples for JavaScript, Python, and more.

56 entries 8 sections

Anchors

Syntax Description Example
Start of string/line ^Hello matches 'Hello world'
End of string/line world$ matches 'Hello world'
Word boundary \bcat\b matches 'cat' not 'catch'
Not a word boundary \Bcat matches 'catch' not 'cat'
Exact full-string match ^hello$ matches only 'hello'

Characters

Syntax Description Example
Any character (except newline) h.t matches 'hat', 'hot'
Any digit (0-9) \d{3} matches '123'
Any non-digit \D+ matches 'abc'
Word character (a-z, 0-9, _) \w+ matches 'hello_42'
Non-word character \W matches '@', '#', ' '
Any whitespace \s+ matches spaces, tabs
Any non-whitespace \S+ matches 'hello'
Match any character in set [aeiou] matches vowels
Match any character NOT in set [^0-9] matches non-digits
Character range [A-Za-z] matches any letter
Digit range [0-9]{2,4} matches 2-4 digits

Quantifiers

Syntax Description Example
0 or more go*gle matches 'ggle', 'google'
1 or more go+gle matches 'gogle', 'google'
0 or 1 (optional) colou?r matches 'color', 'colour'
Exactly n times \d{4} matches '2026'
n or more times \w{3,} matches 3+ word chars
Between n and m times \d{2,4} matches '12', '1234'
0 or more (lazy/non-greedy) <.*?> matches first tag only
1 or more (lazy) \w+? matches single char

Groups

Syntax Description Example
Capturing group (\d{3})-(\d{4}) captures parts
Non-capturing group (?:https?://) groups without capture
Named capturing group (?<year>\d{4})
Back-reference to group 1 (\w+)\s\1 matches 'the the'
Alternation (OR) cat|dog matches 'cat' or 'dog'
Grouped alternation (Dr|Mr)\.? matches 'Dr.' or 'Mr'

Assertions

Syntax Description Example
Positive lookahead \d(?=px) matches '5' in '5px'
Negative lookahead \d(?!px) matches '5' in '5em'
Positive lookbehind (?<=\$)\d+ matches '50' in '$50'
Negative lookbehind (?<!\$)\d+ matches non-price nums

Flags

Syntax Description Example
Global - match all occurrences /cat/g finds all 'cat'
Case-insensitive matching /hello/i matches 'Hello'
Multiline (^ $ per line) /^start/m per line
Dotall (. matches newline) /a.b/s matches 'a\ b'
Unicode support /\u{1F600}/u matches emoji
Extended (ignore whitespace) Allows comments in regex

Escaping

Syntax Description Example
Literal dot 3\.14 matches '3.14'
Literal asterisk \*bold\* matches '*bold*'
Literal backslash C:\\ matches 'C:\'
Literal parenthesis \(text\) matches '(text)'
Newline character line1\ line2
Tab character col1\ col2

Common Patterns

Syntax Description Example
Email validation (basic) user@example.com
URL validation (basic) https://example.com
US phone number 555-123-4567
Date (YYYY-MM-DD) 2026-03-10
Hex color code #ff6600 or #f60
IPv4 address (basic) 192.168.1.1
Password strength (1 upper + 1 digit + 8 chars) P@ssw0rd
Leading/trailing whitespace Use with replace to trim
Repeated characters Matches 'aaa', 'bbb'
Short words (1-3 letters) Matches 'a', 'the', 'is'

Frequently asked questions

What's the difference between greedy and lazy matching?

Greedy (default) matches as much as possible: .* in '<a>text</a>' matches 'a>text</a'. Lazy (*?) matches as little as possible: .*? matches just 'a'. Use lazy quantifiers when matching between delimiters.

How do lookaheads and lookbehinds work?

They're zero-width assertions - they check what's around a position without including it in the match. (?=px) means 'followed by px' and (?<=\$) means 'preceded by $'. They're perfect for matching content in a specific context.

Why doesn't my regex match across lines?

By default, ^ and $ match the start/end of the entire string, and . doesn't match newlines. Use the /m flag for multiline ^ and $, and /s flag to make . match newlines too.

What's the difference between \b and ^/$?

\b matches word boundaries anywhere in the string - the position between a word character and non-word character. ^ and $ match the start and end of the string (or line with /m flag). Use \b for finding whole words, ^/$ for full-string validation.

How do I match special characters literally?

Escape them with a backslash: \. for dot, \* for asterisk, \( for parenthesis. Inside character classes [], most special chars are literal: [.+*] matches those characters literally (except ^, -, and ]).

Can regex validate email addresses properly?

Not really. The full RFC 5322 email regex is thousands of characters long. For practical validation, use a simple pattern like ^[\w.-]+@[\w.-]+\.\w{2,}$ and verify with a confirmation email. Don't rely on regex alone for email validation.

Go from reference to real skills

Cheat sheets are great for quick lookups. Our in-depth courses take you from the fundamentals to professional-level mastery.

Browse all courses