Skip to the content.

Regex Patterns Cheatsheet

Regex Patterns Cheatsheet

A quick-reference guide for Regular Expressions (Regex), covering character classes, anchors, quantifiers, groups, lookaround assertions, regex flags, and common real-world search patterns.

Character Classes

Character classes match specific types of characters.

.                             # Match any single character except newline (\n)
\d                            # Match any digit (0-9) - equivalent to [0-9]
\D                            # Match any non-digit - equivalent to [^0-9]
\w                            # Match any alphanumeric word character (a-z, A-Z, 0-9, _)
\W                            # Match any non-word character (e.g. spaces, symbols)
\s                            # Match any whitespace character (space, tab, newline, carriage return)
\S                            # Match any non-whitespace character

[abc]                         # Match any single character inside the brackets (a, b, or c)
[^abc]                        # Match any single character NOT inside the brackets (not a, b, or c)
[a-z]                         # Match a character in the range a to z
[A-Z]                         # Match a character in the range A to Z
[0-9]                         # Match a character in the range 0 to 9
[a-zA-Z0-9]                   # Match any alphanumeric character

Anchors & Boundaries

Anchors assert positions rather than matching characters.

^                             # Assert start of string (or start of line in multi-line mode)
$                             # Assert end of string (or end of line in multi-line mode)
\b                            # Assert word boundary position (between a word char and a non-word char)
\B                            # Assert non-word boundary position

Quantifiers

Quantifiers specify how many times a character or group must repeat.

*                             # Match 0 or more times
+                             # Match 1 or more times
?                             # Match 0 or 1 time (optional)
{n}                           # Match exactly 'n' times
{n,}                          # Match 'n' or more times
{min,max}                     # Match between 'min' and 'max' times (inclusive)

# Greedy vs Lazy Matching
# By default, quantifiers are greedy (match as much text as possible). Append ? to make them lazy.
*?                            # Match 0 or more times, matching as FEW characters as possible
+?                            # Match 1 or more times, matching as FEW characters as possible

Grouping & Capturing

Groups bundle characters for quantifiers, logic, or extraction.

(abc)                         # Capturing Group: groups characters and records match for backreference
(?:abc)                       # Non-Capturing Group: groups characters but does NOT record match (saves memory)
(?<name>abc)                  # Named Capturing Group: binds group match to a specific name index
\1                            # Backreference: matches the exact same text captured by the first group
\k<name>                      # Named Backreference: matches text captured by group named 'name'
a|b                           # Alternation: match 'a' OR 'b'

Lookaround Assertions

Lookarounds assert that a specific pattern exists (or does not exist) without consuming characters in the match (zero-width assertions).

# Lookahead (Look forward/right)
(?=abc)                       # Positive Lookahead: asserts that 'abc' follows immediately
(?!abc)                       # Negative Lookahead: asserts that 'abc' does NOT follow immediately

# Lookbehind (Look backward/left)
(?<=abc)                      # Positive Lookbehind: asserts that 'abc' precedes immediately
(?<!abc)                      # Negative Lookbehind: asserts that 'abc' does NOT precede immediately

Regex Flags / Modifiers

Flags are set outside the regex delimiters (e.g. /pattern/gi) to modify search behaviors.

g                             # Global search: find all matches rather than stopping at first match
i                             # Case-insensitive search: ignore case shifts
m                             # Multi-line mode: causes ^ and $ to match start/end of lines (split by \n)
s                             # Dotall mode: causes the dot (.) to match newline characters (\n) as well
u                             # Unicode mode: enables full Unicode matching properties
y                             # Sticky search: matches only from the exact index position (lastIndex)

Common Pattern Examples

1. Email Address (Standard RFC 5322)

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

2. Password Strength (Min 8 characters, at least 1 uppercase letter, 1 number, and 1 special symbol)

Uses multiple positive lookaheads to enforce criteria.

^(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$

3. IPv4 Address

Matches digits 0-255 separated by dots.

^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$

4. Date (YYYY-MM-DD)

^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$

5. URL (HTTP, HTTPS, or FTP)

^(?:https?|ftp):\/\/[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=]+$

6. Phone Number (Standard US Formats)

Matches 123-456-7890, (123) 456-7890, 123.456.7890, or 1234567890.

^(?:\(\d{3}\)|\d{3})(?:[ .-]?)?\d{3}(?:[ .-]?)?\d{4}$