01
Basics
Fundamental building blocks of regular expressions
.
Dot
Any character except newline
a.c → abc, aXc, a1c
\
Escape
Escape special character
\. → literal dot
|
Alternation
OR operator
cat|dog → cat or dog
abc
Literals
Exact character sequence
abc → matches "abc"
02
Character Classes
Match specific sets of characters
[abc]
Character Set
Match any character in brackets
[aeiou] → any vowel
[^abc]
Negated Set
Match any character NOT in brackets
[^0-9] → non-digit
[a-z]
Range
Match character in range
[a-zA-Z] → any letter
\d
Digit
Any digit [0-9]
\d{3} → 123, 456
\D
Non-Digit
Any non-digit [^0-9]
\D+ → abc, XYZ
\w
Word Character
Letter, digit, underscore [a-zA-Z0-9_]
\w+ → hello_123
\W
Non-Word
Non-word character [^a-zA-Z0-9_]
\W → @, #, !
\s
Whitespace
Space, tab, newline
a\sb → a b
\S
Non-Whitespace
Any non-whitespace
\S+ → hello
03
Quantifiers
Specify how many times to match
*
Zero or More
Match 0 or more times
ab*c → ac, abc, abbc
+
One or More
Match 1 or more times
ab+c → abc, abbc
?
Optional
Match 0 or 1 time
colou?r → color, colour
{n}
Exact Count
Match exactly n times
\d{4} → 2024
{n,}
At Least
Match n or more times
\d{2,} → 12, 123, 1234
{n,m}
Range
Match between n and m times
\d{2,4} → 12, 123, 1234
*?
Lazy Zero+
Match as few as possible
<.*?> → <tag> (not greedy)
+?
Lazy One+
Match as few as possible
.+? → minimal match
04
Anchors & Boundaries
Match positions, not characters
^
Start of String
Match beginning of string/line
^Hello → "Hello world"
$
End of String
Match end of string/line
world$ → "Hello world"
\b
Word Boundary
Position between word and non-word
\bcat\b → "the cat sat"
\B
Non-Word Boundary
Position NOT at word boundary
\Bcat → "scat" not "cat"
\A
Absolute Start
Very beginning of string
\AStart → only at start
\Z
Absolute End
Very end of string
end\Z → only at end
05
Groups & Capturing
Group patterns and capture matches
(abc)
Capturing Group
Groups and captures the match
(ab)+ → ab, abab
(?:abc)
Non-Capturing
Groups without capturing
(?:ab)+ → groups only
(?<name>abc)
Named Group
Capture with a name
(?<year>\d{4})
\1
Backreference
Reference captured group
(a)\1 → aa
\k<name>
Named Backref
Reference named group
\k<year>
(a|b)
Alternation Group
Match a OR b in group
(cat|dog)s → cats, dogs
06
Lookahead & Lookbehind
Assert what follows or precedes without consuming
(?=abc)
Positive Lookahead
Assert what follows matches
\d(?=px) → 5 in "5px"
(?!abc)
Negative Lookahead
Assert what follows doesn't match
\d(?!px) → 5 in "5em"
(?<=abc)
Positive Lookbehind
Assert what precedes matches
(?<=\$)\d+ → 100 in "$100"
(?<!abc)
Negative Lookbehind
Assert what precedes doesn't match
(?<!\$)\d+ → 100 in "€100"
07
Flags / Modifiers
Change how the pattern is interpreted
g
Global
Find all matches, not just first
/a/g → all "a" occurrences
i
Case Insensitive
Ignore case when matching
/abc/i → ABC, Abc, abc
m
Multiline
^ and $ match line breaks
/^abc/m → per line
s
Dot All
. matches newlines too
/a.b/s → a\nb
u
Unicode
Enable full Unicode support
/\u{1F600}/u → 😀
y
Sticky
Match from lastIndex position
Anchored search
08
Special Characters
Escape sequences and special matchers
\n
Newline
\r
Carriage Return
\t
Tab
\0
Null character
\xhh
Hex character
\uhhhh
Unicode character
Characters to Escape
\
.
*
+
?
^
$
|
{
}
[
]
(
)
/