Regex Cheat Sheet

Every entry below is always listed — search narrows it to patterns, meanings or examples matching what you type.

Anchors

Pattern Meaning Example
^ Start of the string (or of a line, with the m flag) ^Hello matches "Hello world" but not "Say Hello"
$ End of the string (or of a line, with the m flag) world$ matches "Hello world" but not "world peace"
\b Word boundary — between a word character and a non-word character \bcat\b matches "cat" in "the cat sat" but not in "category"
\B NOT a word boundary \Bcat matches "cat" in "concatenate" but not the standalone word "cat"

Character classes

Pattern Meaning Example
. Any character except a line break h.t matches "hot", "hat" and "hit"
\d A digit (0-9) \d{3} matches "123" in "abc123"
\D Any character that is NOT a digit \D+ matches "abc" in "abc123"
\w A word character — letter, digit or underscore \w+ matches "hello_1" in "hello_1!"
\W Any character that is NOT a word character \W matches the space in "a b"
\s A whitespace character — space, tab, newline a\sb matches "a b"
\S Any character that is NOT whitespace \S+ matches "hello" in "hello world"
[abc] Any one of the listed characters [aeiou] matches any single vowel
[^abc] Any character NOT in the list [^0-9] matches any non-digit character
[a-z] Any character in the given range [a-fA-F0-9] matches a single hex digit

Quantifiers

Pattern Meaning Example
* 0 or more of the preceding token ab*c matches "ac", "abc" and "abbbc"
+ 1 or more of the preceding token ab+c matches "abc" and "abbc" but not "ac"
? 0 or 1 of the preceding token — optional colou?r matches both "color" and "colour"
{n} Exactly n repetitions \d{4} matches "2024" but not "202" or "20245"
{n,} n or more repetitions \d{2,} matches "123456" (2 or more digits)
{n,m} Between n and m repetitions, inclusive \d{2,4} matches 2 to 4 digits at a time
*? +? ?? Lazy versions — match as few repetitions as possible <.+?> on "<a><b>" matches "<a>" instead of the whole string

Groups & backreferences

Pattern Meaning Example
(...) Capturing group — remembers what it matched (\d{3})-(\d{4}) captures "555" and "1234" from "555-1234"
(?:...) Non-capturing group — groups without remembering (?:https?|ftp):// matches the scheme without capturing it
(?<name>...) Named capturing group (?<year>\d{4})-(?<month>\d{2}) captures groups named year and month
\1 Backreference to capturing group 1 (\w+)\s\1 matches a repeated word, like "the the"

Lookaround

Pattern Meaning Example
(?=...) Positive lookahead — must be followed by this, not consumed \d+(?=px) matches "42" in "42px" but not in "42em"
(?!...) Negative lookahead — must NOT be followed by this \d+(?!px) matches "42" in "42em" but not in "42px"
(?<=...) Positive lookbehind — must be preceded by this, not consumed (?<=\$)\d+ matches "50" in "$50" but not in "€50"
(?<!...) Negative lookbehind — must NOT be preceded by this (?<!\$)\d+ matches "50" in "€50" but not in "$50"

Flags

Flag Meaning Example
g Global — find all matches, not just the first /o/g on "foo boo" finds both "o"s instead of stopping at the first
i Case-insensitive matching /cat/i matches "cat", "CAT" and "Cat"
m Multiline — ^ and $ match the start/end of each line, not just the whole string /^b/m matches the "b" that starts the second line in "a\nb"
s Dotall — . also matches line-break characters /a.b/s matches "a\nb", which /a.b/ alone does not
u Unicode — treat the pattern as Unicode code points, not UTF-16 code units Needed for \p{...} Unicode property escapes and for matching astral characters (emoji, etc.) correctly
y Sticky — matches only starting at lastIndex, no scanning ahead Used to tokenize a string piece by piece from a known position

Common patterns

Pattern Meaning Example
^[\w.+-]+@[\w-]+\.[a-zA-Z]{2,}$ Email address (a common-case shape, not full RFC 5322) Matches "name.surname+tag@example.co.uk"
^https?:\/\/[\w.-]+(?:\/\S*)?$ HTTP or HTTPS URL, with an optional path Matches "https://example.com/path?query=1"
^(?:(?:25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|1?\d?\d)$ IPv4 address — each octet from 0 to 255 Matches "192.168.1.1" but not "999.1.1.1"
^#(?:[0-9a-fA-F]{3}){1,2}$ Hex color code, 3 or 6 digits, with leading # Matches "#fff" and "#a1b2c3"
^\s+|\s+$ Leading or trailing whitespace, for trimming Replacing this with '' in " hello " gives "hello"

How it works

This is a browsable reference, not a regex tester — it does not run any pattern against text. Type a term (e.g. lookahead) or a literal such as \d to filter the tables above to matching rows; clear the box to see every entry again. To actually test a pattern against your own text, use the Regex Tester tool.