What is a Regular Expression?

Regex explained: character classes, quantifiers, anchors, groups, and practical patterns for everyday use.

7 min read·Updated June 2026

A regular expression (regex) is a sequence of characters that defines a search pattern. You use regex to find, validate, or replace text: check that an email looks valid, extract all URLs from a page, or reformat a date string. Every major programming language has built-in regex support.

The syntax looks cryptic at first, but it follows a handful of rules. Once you know them, you can read most regex patterns in seconds.

Anatomy of a regex

A regex literal is written between two forward slashes, followed by optional flags:

/^\d{3}-\d{4}$/g
/ / delimiters^ $ anchors\d character class{n} quantifier- literalg flag

This pattern matches a 7-digit number in the format 123-4567, from start to end of string.

Regex cheat sheet

Character classes

SymbolMatchesExampleMatches
.Any character (except newline)a.c"abc", "aXc", "a1c"
\dDigit 0–9\d+"0", "42", "2024"
\DNon-digit\D" ", "a", "!"
\wWord char (letter, digit, _)\w+"hello", "a1_b"
\WNon-word char\W" ", "@", "-"
\sWhitespace (space, tab, newline)\s+" ", "\t", "\n"
[abc]Any char in set[aeiou]"a", "e", "i", "o", "u"
[^abc]Any char not in set[^0-9]any non-digit
[a-z]Char range[a-z]{3}"abc", "xyz"

Quantifiers

SymbolMeaningExampleMatches
*0 or morebo*"b", "bo", "boo"
+1 or moreho+"ho", "hoo" (not "h")
?0 or 1colou?r"color", "colour"
{n}Exactly n\d{4}"2024", "0000"
{n,}n or more\d{2,}"12", "123", "9999"
{n,m}Between n and m\w{2,5}2–5 word characters
*? +? ??Lazy (shortest match)<.*?>each tag individually

Anchors & boundaries

SymbolMeaningExampleMatches
^Start of string^Hello"Hello world" (not "Say Hello")
$End of stringend$"the end" (not "endless")
\bWord boundary\bcat\b"cat" but not "catch"
\BNon-word boundary\Bcat"catch" but not "cat"

Groups & alternation

SymbolMeaningExampleMatches
(...)Capturing group(\d{4})-(\d{2})saves year and month
(?:...)Non-capturing group(?:ab)+groups without saving
(?=...)Positive lookahead\d(?= px)"2" in "2 px"
(?!...)Negative lookahead\d(?! px)digit not followed by " px"
a|bAlternation (or)cat|dog"cat" or "dog"

Flags

Flags follow the closing slash and modify how the pattern is applied.

FlagNameEffect
gGlobalFind all matches, not just the first
iCase-insensitiveTreat uppercase and lowercase as equal
mMultiline^ and $ match start/end of each line, not just the whole string
sDot-all. also matches newlines (\n)
uUnicodeEnable full Unicode matching (required for \p{} properties)

Common patterns

Ready-to-use regex for the most frequent validation tasks.

Email (basic)/^[\w.-]+@[\w-]+\.[a-z]{2,}$/i
✅ matchesuser@example.comfirst.last@sub.domain.org
❌ rejects@example.comuser@user @example.com
URL (http/https)/https?:\/\/[\w.-]+\.[a-z]{2,}/i
✅ matcheshttps://example.comhttp://api.site.org/path
❌ rejectsftp://example.comexample.com
ISO date (YYYY-MM-DD)/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/
✅ matches2024-01-312000-12-01
❌ rejects2024-13-0124-01-312024/01/31
Hex color/^#([0-9a-f]{3}|[0-9a-f]{6})$/i
✅ matches#fff#F97316#000000
❌ rejects#gg0000F97316#12345
Integer or decimal/^-?\d+(\.\d+)?$/
✅ matches42-73.14-0.5
❌ rejects3..51,000abc

Greedy vs. lazy matching

By default, quantifiers are greedy — they match as much text as possible. Adding ? after a quantifier makes it lazy — it matches as little as possible.

Greedy .*

Input: <b>bold</b> and <i>italic</i>
Pattern: <.*> → matches the entire string from first < to last >

Lazy .*?

Same input.
Pattern: <.*?> → matches <b>, then </b>, then each tag individually

Frequently asked questions