Regex Cheatsheet
Regular expression syntax reference with common pattern examples.
26 patterns
Literal match
BasicsMatch exact text. Case-sensitive by default.
helloExample: Say hello world
Case insensitive
BasicsThe i flag makes the match case-insensitive.
/hello/iExample: Hello HELLO hello
Any character
Basics. matches any single character except newline.
h.lloExample: hello hallo hxllo
Start of string
Anchors^ anchors the match to the start of the string.
^helloExample: hello world (not: say hello)
End of string
Anchors$ anchors the match to the end of the string.
world$Example: hello world (not: world class)
Word boundary
Anchors\b matches at a word boundary.
\bcat\bExample: cat (not: category, scatter)
0 or more
Quantifiers* matches 0 or more of the preceding element.
ab*cExample: ac, abc, abbc, abbbc
1 or more
Quantifiers+ matches 1 or more of the preceding element.
ab+cExample: abc, abbc (not: ac)
0 or 1
Quantifiers? makes the preceding element optional.
colou?rExample: color, colour
Exactly n times
Quantifiers{n} matches exactly n repetitions.
\d{4}Example: 2024, 1999
Between n and m
Quantifiers{n,m} matches between n and m repetitions.
\d{2,4}Example: 12, 123, 1234
Digit
Character Classes\d matches any digit. \D matches non-digit.
\dExample: 0-9
Word char
Character Classes\w matches word characters. \W matches non-word.
\wExample: a-z, A-Z, 0-9, _
Whitespace
Character Classes\s matches whitespace. \S matches non-whitespace.
\sExample: space, tab, newline
Custom class
Character Classes[] defines a character set. Match any one character listed.
[aeiou]Example: a, e, i, o, u
Negated class
Character Classes^ inside [] negates the set.
[^0-9]Example: any non-digit
Capturing group
Groups() captures matched text for backreferences or extraction.
(\d{4})-(\d{2})-(\d{2})Example: 2024-03-15 → groups: 2024, 03, 15
Non-capturing group
Groups(?:) groups without capturing.
(?:https?://)Example: http:// or https://
Named group
Groups(?<name>) creates a named capture group.
(?<year>\d{4})-(?<month>\d{2})Example: groups.year, groups.month
Alternation (OR)
Groups| acts as OR between alternatives.
cat|dog|birdExample: cat, dog, bird
Lookahead
Lookarounds(?=...) asserts what follows without consuming it.
\d+(?= dollars)Example: 100 in '100 dollars'
Negative lookahead
Lookarounds(?!...) asserts what does NOT follow.
\d+(?! dollars)Example: 100 in '100 euros'
Email pattern
Common PatternsBasic email validation pattern.
[\w.-]+@[\w.-]+\.[a-z]{2,}Example: user@example.com
URL pattern
Common PatternsSimple URL matching pattern.
https?://[\w.-]+(?:\.[a-z]{2,})+(?:/[\S]*)?Example: https://example.com/path
IPv4 address
Common PatternsMatch an IPv4 address (does not validate range 0-255).
(?:\d{1,3}\.){3}\d{1,3}Example: 192.168.1.1
Hex color
Common PatternsMatch hex color codes.
#[0-9A-Fa-f]{3,6}Example: #fff, #3a7bcd
All processing happens locally in your browser. No data is sent to any server.