Regex Cheatsheet
Essential regular expression syntax, patterns, and common use cases for pattern matching.
Useful Regex Resources
- Regex101 - Online regex tester with explanation
- Regexr - Learn, build, and test regex
- RegexOne - Interactive regex tutorial
- Regular-Expressions.info - Comprehensive reference
Basic Patterns
abc
Match exact string "abc"
.
Match any single character (except newline)
\.
Match literal dot (escape special character)
\d
Match any digit (0-9)
\D
Match any non-digit
\w
Match any word character (a-z, A-Z, 0-9, _)
\W
Match any non-word character
\s
Match any whitespace (space, tab, newline)
\S
Match any non-whitespace
Anchors
^
Match start of string/line
$
Match end of string/line
\b
Match word boundary
\B
Match non-word boundary
^hello$
Match exact string "hello" only
\bhello\b
Match "hello" as a whole word
Quantifiers
*
Match 0 or more times (greedy)
+
Match 1 or more times (greedy)
?
Match 0 or 1 time (optional)
{n}
Match exactly n times
{n,}
Match n or more times
{n,m}
Match between n and m times
*?
Match 0 or more times (lazy/non-greedy)
+?
Match 1 or more times (lazy/non-greedy)
Character Classes
[abc]
Match any character in set (a, b, or c)
[^abc]
Match any character NOT in set
[a-z]
Match any lowercase letter
[A-Z]
Match any uppercase letter
[0-9]
Match any digit
[a-zA-Z]
Match any letter
[a-zA-Z0-9]
Match any alphanumeric character
[^0-9]
Match any non-digit
Groups & Alternation
(abc)
Capturing group - match and capture "abc"
(?:abc)
Non-capturing group - match but don't capture
a|b
Alternation - match "a" or "b"
(cat|dog)
Match "cat" or "dog"
(?<name>pattern)
Named capturing group
\1
Backreference - match same text as group 1
(\w+)\s+\1
Match repeated word (e.g., "the the")
Lookahead & Lookbehind
(?=pattern)
Positive lookahead - match if followed by pattern
(?!pattern)
Negative lookahead - match if NOT followed by pattern
(?<=pattern)
Positive lookbehind - match if preceded by pattern
(?<!pattern)
Negative lookbehind - match if NOT preceded by pattern
\d+(?=px)
Match digits followed by "px" (e.g., "100" in "100px")
(?<=\$)\d+
Match digits preceded by "$" (e.g., "50" in "$50")
Common Patterns
Email Address
^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$
URL
https?:\/\/[\w.-]+(?:\/[\w.-]*)*\/?
Phone Number (US)
^\(?[0-9]{3}\)?[-.\s]?[0-9]{3}[-.\s]?[0-9]{4}$
Date (YYYY-MM-DD)
^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$
Time (HH:MM:SS)
^(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d$
IP Address (IPv4)
^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$
Hex Color
^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$
Username (alphanumeric, 3-16 chars)
^[a-zA-Z0-9_]{3,16}$
Password (8+ chars, upper, lower, number)
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$
Slug (URL-friendly)
^[a-z0-9]+(?:-[a-z0-9]+)*$
Flags / Modifiers
i
Case-insensitive matching
g
Global - match all occurrences
m
Multiline - ^ and $ match line boundaries
s
Dotall - . matches newlines too
/pattern/gi
Example: case-insensitive, global match
Escape Sequences
\\
Match backslash
\/
Match forward slash
\.
Match dot
\*
Match asterisk
\+
Match plus
\?
Match question mark
\^
Match caret
\$
Match dollar sign
\[
Match opening bracket
\]
Match closing bracket
\(
Match opening parenthesis
\)
Match closing parenthesis
\{
Match opening brace
\}
Match closing brace
\|
Match pipe
Usage Examples
JavaScript
const regex = /\d+/g;
const text = "I have 3 apples and 5 oranges";
const matches = text.match(regex); // ["3", "5"]
Python
import re
pattern = r'\d+'
text = "I have 3 apples and 5 oranges"
matches = re.findall(pattern, text) # ['3', '5']
Validation Example
const emailRegex = /^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$/;
const isValid = emailRegex.test("user@example.com"); // true
Replace Example
const text = "Hello World";
const result = text.replace(/world/i, "Regex"); // "Hello Regex"



