/^/

Lesson 24 of 28

Regex Cheat Sheet

A compact, categorized recap of the anchors, character classes, quantifiers, groups, lookaround, and flags covered throughout this course.

5 min read

This lesson doesn’t teach anything new — it’s a reference you can scan in a few seconds whenever you’re writing a pattern and can’t quite remember the syntax for a lookbehind or a lazy quantifier. Everything here was covered in earlier lessons; this is the condensed version.

Anchors and boundaries

Token Meaning
^ Start of string (or start of line, with the m flag)
$ End of string (or end of line, with the m flag)
\b Word boundary — between a word character and a non-word character
\B Not a word boundary
123 123abc a123

Character classes

Token Meaning
. Any character except a line break
\d A digit (0-9)
\D Any non-digit
\w A word character (letters, digits, underscore)
\W Any non-word character
\s A whitespace character (space, tab, newline)
\S Any non-whitespace character
[abc] Any one of a, b, or c
[^abc] Any character except a, b, or c
[a-z] Any character in the range a through z

Quantifiers

Token Meaning
* Zero or more (greedy)
+ One or more (greedy)
? Zero or one (greedy); also marks a quantifier as lazy when placed after one
{n} Exactly n times
{n,} n or more times
{n,m} Between n and m times
*?, +?, ?? Lazy versions — match as little as possible
"<a><b>".match(/<.+>/);   // greedy: "<a><b>"
"<a><b>".match(/<.+?>/);  // lazy: "<a>"

Groups

Token Meaning
(...) Capturing group — remembers the matched text by number
(?:...) Non-capturing group — groups for repetition/alternation without recording a capture
(?<name>...) Named capturing group — access via match.groups.name
| Alternation — “this or that”
\1, \2 Backreference to a numbered group’s matched text
\k<name> Backreference to a named group’s matched text
2026-07-10
1 match· capture groups: 3· named groups: year, month, day

Lookaround

Token Meaning
(?=...) Positive lookahead — must be followed by this, not consumed
(?!...) Negative lookahead — must NOT be followed by this
(?<=...) Positive lookbehind — must be preceded by this, not consumed
(?<!...) Negative lookbehind — must NOT be preceded by this
"$100".match(/(?<=\$)\d+/);   // "100" — the $ isn't part of the match
"100px".match(/\d+(?=px)/);   // "100" — the px isn't part of the match
"foo1".match(/foo(?!bar)/);   // "foo" — matches, next chars aren't "bar"

Flags

Flag Name Effect
g Global Find all matches, not just the first
i Case-insensitive Ignore letter case
m Multiline ^/$ match the start/end of each line, not just the whole string
s Dot-all . also matches line breaks
u Unicode Treats the pattern and string as full Unicode code points
"Cat cat CAT".match(/cat/gi);      // ["Cat", "cat", "CAT"]
"line1\nline2".match(/^line/gm);   // ["line", "line"]

Tip

This page is a quick recap, not a replacement for practice. For a searchable, copyable, always-up-to-date version of this same material, check out the full interactive cheat sheet — it’s designed to stay open in a tab while you write real patterns.

Try it yourself

Using only the tables above, write a pattern (without looking back at earlier lessons) that matches a hex color code like #a1b2c3 or #FFF — either 3 or 6 hex digits after a #, anchored so it matches the whole string.

What should happen

One correct answer: ^#(?:[0-9a-fA-F]{3}){1,2}$ doesn’t quite work because it would also accept 3-digit or 6-digit only in multiples of 3, not exactly 3 or 6 with nothing in between — the cleaner form is ^#(?:[0-9a-fA-F]{6}|[0-9a-fA-F]{3})$, an alternation between “exactly 6 hex digits” and “exactly 3 hex digits,” anchored on both ends so nothing shorter or longer sneaks through. This exercise is really testing whether you reach for alternation (|) instead of trying to force a single quantifier to express “3 or 6.”

What’s next

That’s the full course — from “what is a regex” all the way through catastrophic backtracking and production-ready best practices. From here, the best next step is practice: take a real problem from your own work and reach for this cheat sheet, or the interactive version, whenever you need a reminder.

Quick check

Which pair of anchors requires a match to span the entire string, not just part of it?

What's the difference between `(...)`, `(?:...)`, and `(?<name>...)`?