Regex (regular expressions) is one of those tools every developer relies on constantly and nobody fully memorizes. This page is a practical reference: the patterns people search for most, each with a plain explanation and a real example that actually matches. Every pattern below was tested against its example input before publishing, so you can trust it works as described.
Reading Regex at a Glance
A regex pattern is just a compact way of describing a shape of text. A few building blocks come up in almost every pattern:
.matches any single character (except a newline, by default)\dmatches any digit,\wmatches any word character (letters, digits, underscore),\smatches any whitespace[...]defines a custom character class โ a set of characters to match one of^and$anchor a match to the start or end of a string
Combine those with the quantifiers and groups covered further down, and you can describe almost anything.
Validation Patterns
Email address: ^[^\s@]+@[^\s@]+\.[^\s@]+$
Matches any string with characters before an @, more characters after it, a dot, then more characters โ with no whitespace or extra @ signs anywhere. Example: matches jane.doe@example.com but not not-an-email. Worth being honest about: a fully RFC-5322-compliant email pattern is notoriously long and complex (email addresses technically allow far stranger formats than most people realize), so this simpler pattern isn't bulletproof โ but it correctly handles the vast majority of real-world addresses, which is what most validation actually needs.
URL: https?:\/\/[^\s]+
Matches "http://" or "https://" (the ? makes the "s" optional) followed by any run of non-whitespace characters. Example: matches https://example.com/page?id=1 inside the sentence "Visit https://example.com/page?id=1 for more."
US phone number: ^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$
Matches an optional opening parenthesis, 3 digits, optional closing parenthesis, an optional separator (dash, dot, or space), 3 more digits, another optional separator, then 4 digits. Example: matches both (555) 123-4567 and 555-123-4567.
Extraction & Matching Patterns
Digits only: \d+
Matches one or more consecutive digits โ useful for pulling numbers out of a larger string. Example: run against "Order #4821, qty: 3" with the global flag and you get 4821 and 3 as two separate matches.
Whitespace: \s+
Matches one or more consecutive whitespace characters โ spaces, tabs, or newlines. Commonly used to split text into words or collapse repeated spaces. Example: matches the gaps in "a b\tc\nd" โ a run of 3 spaces, a tab, and a newline are each one match.
Word boundary: \bcat\b
\b matches the invisible position between a word character and a non-word character, without consuming any characters itself. This is what lets you match a whole word without accidentally matching it inside a longer word. Example: \bcat\b matches "cat" in "the cat sat" but does not match "cat" inside "category."
\b will silently match "category," "concatenate," and "scatter" too.
Character Classes & Anchors
Character class: [a-z] matches any single lowercase letter a through z. Stack a quantifier on it, like [a-z]+, to match a whole run of lowercase letters โ this matches all of "hello."
Negated class: [^0-9] โ the ^ right after the opening bracket flips the meaning to "anything except." [^0-9]+ matches "abc" at the start of "abc123," stopping as soon as it hits a digit.
Anchors: ^Hello only matches if "Hello" is at the very start of the string; world$ only matches if "world" is at the very end. Both match against "Hello world" โ the first at the beginning, the second at the end.
Quantifiers
Quantifiers control how many times the thing before them can repeat:
+means "one or more."go+glematches "google" and also "gooogle."*means "zero or more."go*glematches "ggle" (zero o's) all the way up to "goooogle."?means "zero or one" โ makes the preceding thing optional.colou?rmatches both "color" and "colour."
Groups: Capturing vs Non-Capturing
Capturing group: (\d{4})-(\d{2})-(\d{2}) wraps parts of a pattern in parentheses so you can extract them individually afterward โ this matches a date like "2026-08-16" and gives you the year, month, and day as three separate captured values.
Non-capturing group: (?:https?:\/\/)?(\w+\.com) uses (?:...) when you need parentheses purely to group parts of a pattern together (here, to make the whole protocol optional) without wanting that piece returned as a captured value โ only the (\w+\.com) part is captured. This matters when you have several groups and only care about extracting some of them.
Flags
Flags go after the closing slash of a pattern and change how matching behaves overall:
g(global) โ find every match in the string, not just the first one./cat/gagainst "cat cat cat" returns all three matches instead of stopping after the first.i(case-insensitive) โ ignore uppercase/lowercase differences./hello/imatches "HELLO world."m(multiline) โ makes^and$match the start/end of each line rather than only the start/end of the whole string./^line/gmagainst a 3-line string matches "line" at the start of each of the first two lines.
FAQ
Why doesn't my email regex catch every valid address? Because a fully spec-compliant email pattern is extremely long and rarely worth the complexity โ the simpler pattern on this page covers essentially every address you'll encounter in practice. For anything security-critical, pair regex validation with an actual confirmation email.
What's the difference between greedy and lazy matching? By default, quantifiers like + and * are "greedy" โ they match as much text as possible. Adding a ? after them (like +?) makes them "lazy," matching as little as possible instead. This matters most when matching things like quoted strings or HTML tags.
Do I need to escape special characters? Yes โ characters like ., *, +, ?, (, and ) have special meaning in regex, so to match them literally you need a backslash before them, like \. to match an actual period.
Is regex the same across JavaScript, Python, and other languages? Mostly, but not identical โ the core syntax is very similar, but some features (like named groups or lookbehind support) vary by language and even by engine version. Always test your pattern in the actual environment you'll use it in.