Regex Cheat Sheet: The Most Useful Patterns for Developers

This cheat sheet covers common JavaScript-style regular-expression syntax and illustrative patterns. Engine support and validation requirements differ; test patterns with valid, invalid, and long inputs before relying on them.

Core Syntax Quick Reference

Character Classes

.        Any character except newline
\d       Digit [0-9]
\D       Non-digit [^0-9]
\w       Word character [a-zA-Z0-9_]
\W       Non-word character
\s       Whitespace (space, tab, newline)
\S       Non-whitespace
[abc]    Any of a, b, or c
[^abc]   Not a, b, or c
[a-z]    Range: a through z

Quantifiers

*        0 or more
+        1 or more
?        0 or 1 (optional)
{3}      Exactly 3
{2,5}    Between 2 and 5
{3,}     3 or more

Anchors & Boundaries

^        Start of string (or line with m flag)
$        End of string (or line with m flag)
\b       Word boundary
\B       Not a word boundary

Groups & Alternation

(abc)      Capture group
(?:abc)    Non-capturing group
(?<name>abc)  Named capture group
a|b        a or b (alternation)
\1         Back-reference to group 1

Lookahead & Lookbehind

(?=abc)    Positive lookahead (followed by abc)
(?!abc)    Negative lookahead (NOT followed by abc)
(?<=abc)   Positive lookbehind (preceded by abc)
(?<!abc)   Negative lookbehind (NOT preceded by abc)

Flags

g    Global — find all matches, not just the first
i    Case-insensitive
m    Multiline — ^ and $ match line starts/ends
s    Dotall — . matches newline characters too
u    Unicode support

Practical Patterns

Email Address (Simple)

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

Matches most common email formats. For production validation, prefer your framework's built-in email validator or actually send a confirmation email — regex alone can't verify deliverability.

URL

^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&\/=]*)$

This illustrative pattern matches a limited subset of HTTP(S) URL text. It rejects valid URLs and may accept invalid ones; use a URL parser and explicit scheme/host rules for application validation.

IPv4 Address

^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$

The pattern checks octet values from 0 to 255 but accepts leading zeros. If canonical dotted-decimal IPv4 is required, use a suitable parser or stricter rules.

IPv6 Address (Simplified)

^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$

Matches full-form IPv6. Compressed forms (with ::) require a more complex pattern.

Date: YYYY-MM-DD

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$

Validates ISO 8601 date format. Note: this doesn't catch invalid dates like Feb 31 — use a date library for that.

Time: HH:MM (24-hour)

^([01]\d|2[0-3]):[0-5]\d$

Phone Number (US)

^(\+1[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$

This illustrative US-style pattern matches several formats, but its optional parentheses can be unbalanced. It does not establish number assignment, reachability, or international validity.

Hex Color Code

^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$

Matches both shorthand (#fff) and full (#ffffff) hex colors.

Password Composition Check (Not Strength)

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$

This example enforces a particular character-composition policy; it does not measure entropy, uniqueness, compromised-password status, or resistance to guessing. Do not treat a regex match as password strength.

HTML Tags

<([a-z][a-z0-9]*)\b[^>]*>(.*?)<\/\1>

Matches opening and closing HTML tags with content. The \1 back-reference ensures the closing tag matches the opening tag.

Warning: Don't use regex to parse HTML in production. HTML is not a regular language — use a proper DOM parser instead. Regex is fine for quick search-and-replace in your editor, though.

Whitespace Trimming

^\s+|\s+$

Matches leading and trailing whitespace. Useful for cleanup, though most languages have a built-in trim() method.

Duplicate Words

\b(\w+)\s+\1\b

Finds repeated words like "the the" or "is is". Great for proofreading.

Extract Numbers from Text

-?\d+\.?\d*

Matches some integers and decimal spellings, including negative numbers. It does not cover leading-dot fractions, exponents, locale separators, or complete numeric validation.

Regex Performance Tips

  • Be specific about intended characters, but do not assume [a-z]+ is inherently faster than .+. Engine optimizations and input determine performance.
  • Avoid catastrophic backtracking — Patterns like (a+)+ can take exponential time on certain inputs. Use atomic groups or possessive quantifiers when available.
  • Use non-capturing groups when captured text is unnecessary. Any performance effect depends on the engine and workload.
  • Anchor when possible — ^pattern$ tells the engine it only needs to check from the start, avoiding unnecessary scanning.
  • Compile once, use many times — In languages like Python (re.compile) or Java (Pattern.compile), compile your regex once and reuse it.

Try It Yourself

Test any of these patterns with live highlighting using the Regex Tester — paste your pattern and test string, and see matches instantly in your browser.

Want to test a pattern?

Open Regex Tester →