Regular Expressions Cheat Sheet

Intermediate | 15 min read | 2025.01.10

Basic Metacharacters

PatternDescriptionExample
.Any single character (except newline)a.c -> abc, aXc
^Start of line^Hello
$End of lineWorld$
\Escape character\. -> literal period
|OR (either)cat|dog

Character Classes

PatternDescriptionExample
[abc]Any of a, b, or c[aeiou] -> vowels
[^abc]Not a, b, or c[^0-9] -> non-digit
[a-z]Range a to z[A-Za-z] -> letters
[0-9]Range 0 to 9[0-9]+ -> digits

Special Character Classes

PatternDescriptionEquivalent
\dDigit[0-9]
\DNon-digit[^0-9]
\wWord character[A-Za-z0-9_]
\WNon-word character[^A-Za-z0-9_]
\sWhitespace[ \t\n\r\f]
\SNon-whitespace[^ \t\n\r\f]
\bWord boundary\bword\b
\BNon-word boundary

Quantifiers

PatternDescriptionExample
*0 or morea* -> "", a, aa, aaa
+1 or morea+ -> a, aa, aaa
?0 or 1colou?r -> color, colour
{n}Exactly n timesa{3} -> aaa
{n,}n or more timesa{2,} -> aa, aaa, aaaa
{n,m}n to m timesa{2,4} -> aa, aaa, aaaa

Lazy (Non-Greedy) Matching

PatternDescription
*?0 or more (lazy)
+?1 or more (lazy)
??0 or 1 (lazy)
{n,m}?n to m times (lazy)
// Greedy (default)
"<div>a</div><div>b</div>".match(/<div>.*<\/div>/);
// "<div>a</div><div>b</div>"

// Non-greedy
"<div>a</div><div>b</div>".match(/<div>.*?<\/div>/);
// "<div>a</div>"

Grouping

PatternDescriptionExample
(abc)Capture group(\d+)-(\d+)
(?:abc)Non-capturing group(?:https?|ftp)://
(?<name>abc)Named group(?<year>\d{4})
\1, \2Backreference(\w+)\s\1 -> word word
// Capture groups
const match = "2025-01-10".match(/(\d{4})-(\d{2})-(\d{2})/);
// match[1] = "2025", match[2] = "01", match[3] = "10"

// Named groups
const match = "2025-01-10".match(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/);
// match.groups.year = "2025"

Lookahead and Lookbehind

PatternDescriptionExample
(?=abc)Positive lookahead\d+(?=px) -> “100”px
(?!abc)Negative lookahead\d+(?!px) -> 100em
(?<=abc)Positive lookbehind(?<=\$)\d+ -> $“100”
(?<!abc)Negative lookbehind(?<!\$)\d+
// Lookahead: digits followed by "px"
"100px 200em".match(/\d+(?=px)/g);  // ["100"]

// Lookbehind: digits preceded by "$"
"$100 200px".match(/(?<=\$)\d+/g);  // ["100"]

Flags

FlagDescription
gGlobal (match all occurrences)
iCase-insensitive
mMultiline mode (^ and $ match each line)
sDotall (. matches newlines)
uUnicode support
// Multiple flags
/pattern/gi

// Case-insensitive
"Hello WORLD".match(/hello/i);  // ["Hello"]

// Global
"a1b2c3".match(/\d/g);  // ["1", "2", "3"]

Common Patterns

Email Address

^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$

URL

https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)

Phone Number (Japan)

0\d{1,4}-?\d{1,4}-?\d{4}

Postal Code (Japan)

^\d{3}-?\d{4}$

Date (YYYY-MM-DD)

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

IP Address

^(?:(?: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]?)$

Password (8+ chars, uppercase, lowercase, and digits)

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$

HTML Tags

<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)

Full-Width Characters

[^\x01-\x7E]

Japanese Characters (Hiragana, Katakana, Kanji)

[\u3040-\u309F\u30A0-\u30FF\u4E00-\u9FAF]

Usage in JavaScript

// Test
/\d+/.test("abc123");  // true

// Match
"hello123world456".match(/\d+/g);  // ["123", "456"]

// Replace
"hello world".replace(/world/, "JavaScript");
"a1b2c3".replace(/\d/g, "X");  // "aXbXcX"

// Split
"a,b;c".split(/[,;]/);  // ["a", "b", "c"]

// Search
"hello world".search(/world/);  // 6

Usage in Python

import re

# Match
re.match(r"\d+", "123abc")  # Start of string only
re.search(r"\d+", "abc123")  # First occurrence
re.findall(r"\d+", "a1b2c3")  # All occurrences

# Replace
re.sub(r"\d+", "X", "a1b2c3")  # "aXbXcX"

# Split
re.split(r"[,;]", "a,b;c")  # ["a", "b", "c"]

# Compile
pattern = re.compile(r"\d+")
pattern.findall("a1b2c3")
← Back to list