Regex Cheat Sheet — Complete Regular Expression Reference

Your go-to quick reference for regular expression syntax. Browse by category, find the syntax you need, and click to copy. Covers characters, quantifiers, anchors, groups, and more.

Characters

SyntaxDescription
.Any character except newline
\dDigit (0-9)
\wWord character (a-z, A-Z, 0-9, _)
\sWhitespace (space, tab, newline)
\DNon-digit
\WNon-word character
\SNon-whitespace

Quantifiers

SyntaxDescription
*Zero or more
+One or more
?Zero or one
{n}Exactly n times
{n,}n or more times
{n,m}Between n and m times
*?Zero or more (lazy)
+?One or more (lazy)

Anchors

SyntaxDescription
^Start of string/line
$End of string/line
\bWord boundary
\BNon-word boundary

Groups

SyntaxDescription
(…)Capturing group
(?:…)Non-capturing group
(?=…)Positive lookahead
(?!…)Negative lookahead
(?<=…)Positive lookbehind
(?<!…)Negative lookbehind
(?<name>…)Named capturing group

Character Classes

SyntaxDescription
[abc]Match a, b, or c
[^abc]Match anything except a, b, or c
[a-z]Match a through z
[a-zA-Z]Match a-z or A-Z

Flags

SyntaxDescription
gGlobal (match all)
iCase-insensitive
mMultiline (^ and $ match lines)
sDotall (. matches newline)
uUnicode support

Special

SyntaxDescription
|Alternation (OR)
\Escape special character
(…)\1Back-reference to group 1

How to Use This Regex Cheat Sheet

This cheat sheet is organized into seven categories of regular expression syntax. Each entry shows the syntax on the left and a plain English description on the right. Click any row to copy the syntax to your clipboard — ready to paste into your pattern.

If you're new to regex, start with the Characters and Quantifiers sections. These building blocks form the foundation of most patterns. Once you're comfortable with those, explore Anchors, Groups, and Character Classes for more advanced matching capabilities.

Character Classes Explained

Character classes let you match specific types of characters without listing them all individually. The most commonly used are the shorthand classes:

  • \dMatches any digit from 0 to 9. Equivalent to [0-9].
  • \wMatches any word character: letters (a-z, A-Z), digits (0-9), and underscore (_). Equivalent to [a-zA-Z0-9_].
  • \sMatches any whitespace character: space, tab, newline, carriage return, form feed.

The uppercase versions (\D, \W, \S) match the opposite — any character that is NOT a digit, word character, or whitespace. Custom character classes use square brackets: [abc] matches a, b, or c; [a-z] matches any lowercase letter; [^abc] matches anything except a, b, or c.

Quantifiers: Controlling Match Count

Quantifiers control how many times the preceding element must appear for a match to succeed. They are essential for building flexible patterns:

* — Zero or more

Matches the preceding element zero or more times. ab*c matches ac, abc, abbc, etc.

+ — One or more

Matches the preceding element one or more times. ab+c matches abc, abbc, but NOT ac.

? — Zero or one

Makes the preceding element optional. colou?r matches both color and colour.

{n,m} — Between n and m

Matches the preceding element between n and m times. \d{2,4} matches 2 to 4 digits.

Anchors: Matching Positions

Unlike other regex tokens, anchors don't match characters — they match positions in the string. This makes them essential for enforcing where a pattern must occur:

  • ^Matches the start of the string (or start of a line in multiline mode). Use it to ensure a pattern appears at the beginning.
  • $Matches the end of the string (or end of a line in multiline mode). Use it to ensure a pattern appears at the end.
  • \bMatches a word boundary — the position between a word character and a non-word character. Useful for matching whole words.

For example, ^\d+$ ensures the entire string consists only of digits. Without the anchors, \d+ would match digits anywhere in the string.

Groups and Lookaround Assertions

Groups serve two purposes: they organize patterns into logical units and they capture matched text for later use. Understanding the different group types is crucial for advanced regex:

  • (...)Capturing group. Saves the matched text so you can reference it with $1, $2, etc. in substitution or backreferences.
  • (?:...)Non-capturing group. Groups the pattern without saving the match. Use when you need grouping but don't need the captured text.
  • (?=...)Positive lookahead. Asserts that what follows matches the pattern, without consuming characters.
  • (?!...)Negative lookahead. Asserts that what follows does NOT match the pattern.

Regex Flags Reference

Flags modify how the regex engine interprets and executes a pattern. In JavaScript, flags are appended after the closing slash:/pattern/flags

g

Global

Find all matches instead of stopping at the first one.

i

Case-Insensitive

Ignore case when matching. [a-z] and [A-Z] become equivalent.

m

Multiline

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

s

DotAll

The dot (.) matches newline characters too, allowing patterns to span lines.

u

Unicode

Enable full Unicode support including Unicode property escapes.

Tips for Writing Better Regex

Be specific with character classes

Use \d instead of [0-9], \w instead of [a-zA-Z0-9_]. Shorthand classes are shorter, more readable, and less error-prone.

Use anchors to avoid partial matches

Without ^ and $, your pattern may match substrings you didn't intend. Always consider whether you need to match the entire string.

Prefer non-capturing groups when possible

Use (?:...) instead of (...) when you don't need the captured text. It's more efficient and makes your intent clear.

Watch out for greedy quantifiers

By default, quantifiers are greedy — they match as much as possible. Add ? after a quantifier (*?, +?) to make it lazy.

Test with edge cases

Always test your regex with empty strings, strings with special characters, and very long strings to catch unexpected behavior.

Need to Test a Pattern?

Use the cheat sheet to build your regex, then test it instantly with the Regex Shuttle tester.