DTTooleras

Regular Expressions Cheat Sheet: From Beginner to Advanced

A comprehensive regex reference covering basic patterns, quantifiers, groups, lookaheads, and real-world examples in JavaScript, Python, and other languages. Bookmark this one.

DevToolsHub Team18 min read938 words

What Are Regular Expressions?

Regular expressions (regex or regexp) are patterns used to match character combinations in strings. They are one of the most powerful tools in a developer's toolkit, used for validation, search-and-replace, data extraction, and text processing.

Every major programming language supports regex, though syntax can vary slightly between implementations (PCRE, JavaScript, Python, etc.).

Basic Patterns

Literal Characters

The simplest regex is a literal string. The pattern hello matches the exact string "hello" in the input text.

Character Classes

Character classes match any single character from a set:

PatternMatches
[abc]Any one of a, b, or c
[a-z]Any lowercase letter
[A-Z]Any uppercase letter
[0-9]Any digit
[a-zA-Z0-9]Any alphanumeric character
[^abc]Any character except a, b, or c

Predefined Character Classes

PatternEquivalentMatches
\d[0-9]Any digit
\D[^0-9]Any non-digit
\w[a-zA-Z0-9_]Any word character
\W[^a-zA-Z0-9_]Any non-word character
\s[ \t\n\r\f]Any whitespace
\S[^ \t\n\r\f]Any non-whitespace
.(almost anything)Any character except newline

Quantifiers

Quantifiers specify how many times a pattern should match:

QuantifierMeaning
*Zero or more times
+One or more times
?Zero or one time
{n}Exactly n times
{n,}n or more times
{n,m}Between n and m times

Greedy vs Lazy

By default, quantifiers are greedy — they match as much as possible. Add ? to make them lazy (match as little as possible):

Greedy:  /<.*>/   on "<b>hello</b>" matches "<b>hello</b>"
Lazy:    /<.*?>/  on "<b>hello</b>" matches "<b>"

Anchors

Anchors don't match characters — they match positions:

AnchorMatches
^Start of string (or line with m flag)
$End of string (or line with m flag)
\bWord boundary
\BNon-word boundary
// Word boundary example
/\bcat\b/.test("the cat sat")    // true
/\bcat\b/.test("concatenate")    // false

Groups and Capturing

Capturing Groups

Parentheses create capturing groups that extract matched text:

const match = "2024-01-15".match(/(\d{4})-(\d{2})-(\d{2})/);
// match[0] = "2024-01-15" (full match)
// match[1] = "2024" (year)
// match[2] = "01" (month)
// match[3] = "15" (day)

Named Groups

Use (?<name>...) for more readable captures:

const match = "2024-01-15".match(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/);
// match.groups.year = "2024"
// match.groups.month = "01"
// match.groups.day = "15"

Non-Capturing Groups

Use (?:...) when you need grouping but don't need to capture:

// Matches "http" or "https" without capturing
/(?:https?)://.test("https://example.com"); // true

Lookaheads and Lookbehinds

These are zero-width assertions — they check what's ahead or behind without consuming characters:

PatternNameMeaning
(?=...)Positive lookaheadFollowed by ...
(?!...)Negative lookaheadNOT followed by ...
(?<=...)Positive lookbehindPreceded by ...
(?<!...)Negative lookbehindNOT preceded by ...
// Password must contain at least one digit and one uppercase letter
/^(?=.*\d)(?=.*[A-Z]).{8,}$/

// Match a number NOT followed by "px"
/\d+(?!px)/

// Match a dollar amount (number preceded by $)
/(?<=\$)\d+(\.\d{2})?/

Flags

FlagNameEffect
gGlobalFind all matches, not just the first
iCase-insensitiveIgnore case when matching
mMultiline^ and $ match line boundaries
sDotall. matches newline characters
uUnicodeEnable full Unicode support
yStickyMatch from lastIndex position only

Real-World Examples

Email Validation (Basic)

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

Note: Perfect email validation via regex is nearly impossible. For production, use a library or send a verification email.

URL Matching

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

Phone Number (US)

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

IP Address (IPv4)

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

HTML Tag Extraction

/<(\w+)[^>]*>(.*?)<\/\1>/gs

Password Strength

// At least 8 chars, 1 uppercase, 1 lowercase, 1 digit, 1 special char
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/

Extracting Numbers from Text

const text = "The price is $42.99 and shipping is $5.00";
const prices = text.match(/\$\d+\.\d{2}/g);
// ["$42.99", "$5.00"]

Common Mistakes

  1. Not escaping special characters — Characters like ., *, +, ?, (, ), [, ], {, }, ^, $, |, \ have special meaning and must be escaped with \ when used literally.

  2. Catastrophic backtracking — Nested quantifiers like (a+)+ can cause exponential time complexity. Always test your regex with edge cases.

  3. Overusing regex — Sometimes a simple string.includes() or string.startsWith() is clearer and faster than a regex.

  4. Forgetting anchors — Without ^ and $, your pattern might match a substring when you intended to match the entire string.

Performance Tips

  • Use non-capturing groups (?:...) when you don't need the captured value
  • Avoid nested quantifiers
  • Use specific character classes instead of . when possible
  • Compile regex once and reuse (in languages that support it)
  • Use test() instead of match() when you only need a boolean result

Try your regex patterns with our Regex Tester tool — it shows matches in real-time with capture group highlighting.

regexregular expressionsregex cheat sheetregex tutorialregex patternsregex examples

Related articles

All articles

Practice with free tools

200+ free developer tools that run in your browser.

Browse all tools →