Regex Cheat Sheet

Complete reference for regular expression syntax β€” anchors, character classes, quantifiers, groups, lookarounds, flags and more. Click any symbol to test it in the playground.

Anchors

^

Start of string

Matches the beginning of the string (or line in multiline mode)

^hello

"hello world" β†’ matches "hello"

$

End of string

Matches the end of the string (or line in multiline mode)

world$

"hello world" β†’ matches "world"

\b

Word boundary

Matches a position between a word and non-word character

\bcat\b

"the cat sat" β†’ matches "cat"

\B

Non-word boundary

Matches a position that is NOT a word boundary

\Bcat\B

"concatenate" β†’ matches "cat"

\A

Start of string (strict)

Matches only at the very start, never at line breaks

\Ahello

"hello world" β†’ matches "hello"

\Z

End of string (strict)

Matches only at the very end, ignoring final newline

world\Z

"hello world" β†’ matches "world"

Character Classes

.

Any character

Matches any character except newline (use /s flag to include newline)

c.t

"cat", "cot", "cut" β†’ all match

\d

Digit

Matches any digit [0-9]

\d+

"abc123" β†’ matches "123"

\D

Non-digit

Matches any character that is NOT a digit

\D+

"abc123" β†’ matches "abc"

\w

Word character

Matches [a-zA-Z0-9_]

\w+

"hello_world" β†’ matches "hello_world"

\W

Non-word character

Matches any character NOT in [a-zA-Z0-9_]

\W+

"hello world!" β†’ matches " " and "!"

\s

Whitespace

Matches space, tab, newline, carriage return, form feed

\s+

"hello world" β†’ matches " "

\S

Non-whitespace

Matches any character that is NOT whitespace

\S+

"hello world" β†’ matches "hello", "world"

\t

Tab

Matches a horizontal tab character

col1\tcol2

"col1\tcol2" β†’ matches full string

\n

Newline

Matches a newline character

line1\nline2

Matches across lines

\r

Carriage return

Matches a carriage return character

\r\n

Windows line endings

Quantifiers

*

Zero or more (greedy)

Matches 0 or more of the preceding element, as many as possible

ab*c

"ac", "abc", "abbc" β†’ all match

+

One or more (greedy)

Matches 1 or more of the preceding element, as many as possible

ab+c

"abc", "abbc" β†’ match; "ac" β†’ no match

?

Zero or one (greedy)

Matches 0 or 1 of the preceding element (makes it optional)

colou?r

"color", "colour" β†’ both match

{n}

Exactly n times

Matches exactly n occurrences of the preceding element

\d{4}

"2024" β†’ matches; "123" β†’ no match

{n,}

n or more times

Matches n or more occurrences of the preceding element

\d{2,}

"12", "123", "1234" β†’ all match

{n,m}

Between n and m times

Matches between n and m occurrences (inclusive)

\d{2,4}

"12", "123", "1234" β†’ match; "1" β†’ no match

*?

Zero or more (lazy)

Matches 0 or more, as few as possible

<.*?>

"<a><b>" β†’ matches "<a>" then "<b>"

+?

One or more (lazy)

Matches 1 or more, as few as possible

<.+?>

"<ab>" β†’ matches "<ab>" minimally

??

Zero or one (lazy)

Matches 0 or 1, preferring 0

colou??r

Prefers "color" over "colour"

Groups & References

(abc)

Capture group

Groups the expression and captures the match for back-references

(\d{4})-(\d{2})

"2024-05" β†’ group 1: "2024", group 2: "05"

(?:abc)

Non-capture group

Groups the expression without capturing (more efficient)

(?:abc)+

"abcabc" β†’ matches whole string

(?<name>abc)

Named capture group

Captures with a name for easier reference

(?<year>\d{4})

"2024" β†’ group "year": "2024"

\1

Back-reference

Refers to the text matched by capture group 1

(\w+) \1

"hello hello" β†’ matches; "hello world" β†’ no match

\k<name>

Named back-reference

Refers to the text matched by a named capture group

(?<w>\w+) \k<w>

"hello hello" β†’ matches

(a|b)

Alternation in group

Matches either a or b inside a group

(cat|dog)s?

"cat", "cats", "dog", "dogs" β†’ all match

Lookahead & Lookbehind

(?=abc)

Positive lookahead

Matches a position followed by the pattern (without consuming it)

\d+(?= dollars)

"100 dollars" β†’ matches "100"

(?!abc)

Negative lookahead

Matches a position NOT followed by the pattern

\d+(?! dollars)

"100 euros" β†’ matches "100"

(?<=abc)

Positive lookbehind

Matches a position preceded by the pattern (without consuming it)

(?<=\$)\d+

"$100" β†’ matches "100"

(?<!abc)

Negative lookbehind

Matches a position NOT preceded by the pattern

(?<!\$)\d+

"€100" β†’ matches "100"

Character Sets

[abc]

Character set

Matches any single character in the set

[aeiou]

"hello" β†’ matches "e", "o"

[^abc]

Negated set

Matches any single character NOT in the set

[^aeiou]

"hello" β†’ matches "h", "l", "l"

[a-z]

Range

Matches any character in the range (lowercase letters)

[a-z]+

"Hello123" β†’ matches "ello"

[A-Z]

Uppercase range

Matches any uppercase letter

[A-Z]+

"Hello World" β†’ matches "H", "W"

[0-9]

Digit range

Matches any digit (equivalent to \d)

[0-9]+

"abc123" β†’ matches "123"

[a-zA-Z]

Letter range

Matches any letter (upper or lowercase)

[a-zA-Z]+

"Hello123" β†’ matches "Hello"

[a-zA-Z0-9]

Alphanumeric

Matches any letter or digit

[a-zA-Z0-9]+

"Hello 123!" β†’ matches "Hello", "123"

Flags / Modifiers

g

Global

Find all matches instead of stopping after the first one

/\d+/g

"a1b2c3" β†’ matches "1", "2", "3"

i

Case-insensitive

Makes the match case-insensitive

/hello/i

"Hello", "HELLO", "hello" β†’ all match

m

Multiline

^ and $ match start/end of each line, not just the string

/^\w+/gm

Matches first word on each line

s

Dot-all

Makes dot (.) match newline characters as well

/a.b/s

"a\nb" β†’ matches

u

Unicode

Enables full Unicode matching and support for \u{} escapes

/\u{1F600}/u

Matches emoji and Unicode chars properly

y

Sticky

Matches only from the position indicated by lastIndex property

/\d+/y

Anchors match to lastIndex position

d

Indices

Provides indices (start/end positions) for captured groups

/(?<g>\w+)/d

Returns match.indices for each group

Special & Escape

|

Alternation

Matches either the expression before or after the |

cat|dog

"I have a cat" or "I have a dog" β†’ match

\

Escape

Escapes a special character to match it literally

\.

"3.14" β†’ matches "." literally

(?#comment)

Comment

Adds a comment inside the regex (ignored by engine)

\d+(?#digits)

Same as \d+

\0

Null character

Matches the null character (U+0000)

\0

Matches null byte

\uXXXX

Unicode escape

Matches a Unicode character by code point

\u0041

Matches "A"

\xHH

Hex escape

Matches a character by hexadecimal code

\x41

Matches "A"

Ready to test a pattern?

Open the playground, paste any regex from this cheat sheet, and see real-time matches with visual highlighting.

Open Playground