DEV Community

arenasbob2024-cell
arenasbob2024-cell

Posted on • Originally published at viadreams.cc

The Complete Regex Cheat Sheet for 2026

Why Every Developer Needs Regex

Regular expressions (regex) are one of the most powerful tools in a developer's toolkit. Whether you are validating user input, parsing log files, or performing search-and-replace operations, regex lets you match complex text patterns with concise syntax. This cheat sheet covers everything from the basics to advanced techniques.

Basic Character Classes

Pattern Matches Example
. Any character except newline a.c matches abc, a1c
\d Any digit (0-9) \d{3} matches 123
\D Any non-digit \D+ matches abc
\w Word character (a-z, A-Z, 0-9, _) \w+ matches hello_42
\W Non-word character \W matches @
\s Whitespace (space, tab, newline) \s+ matches spaces
\S Non-whitespace \S+ matches hello
[abc] Any of a, b, or c [aeiou] matches vowels
[^abc] Not a, b, or c [^0-9] matches non-digits
[a-z] Range: a through z [A-Za-z] matches letters

Quantifiers

Pattern Meaning Example
* 0 or more ab*c matches ac, abc, abbc
+ 1 or more ab+c matches abc, abbc but not ac
? 0 or 1 (optional) colou?r matches color, colour
{n} Exactly n times \d{4} matches 2026
{n,} n or more times \d{2,} matches 42, 123
{n,m} Between n and m times \w{3,8} matches 3-8 word chars
*? Lazy (minimal) match ".*?" matches shortest quoted string

Anchors and Boundaries

^        Start of string (or line in multiline mode)
$        End of string (or line in multiline mode)
\b       Word boundary
\B       Non-word boundary
Enter fullscreen mode Exit fullscreen mode

Example:

// Match whole words only
const regex = /\bfunction\b/g;
"function myFunction() {}".match(regex);
// Returns: ["function"]  (not the one inside myFunction)
Enter fullscreen mode Exit fullscreen mode

Groups and Capturing

Capturing Groups

const dateRegex = /(\d{4})-(\d{2})-(\d{2})/;
const match = "2026-02-22".match(dateRegex);

console.log(match[0]); // "2026-02-22" (full match)
console.log(match[1]); // "2026" (year)
console.log(match[2]); // "02"   (month)
console.log(match[3]); // "22"   (day)
Enter fullscreen mode Exit fullscreen mode

Named Groups

const regex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const { groups } = "2026-02-22".match(regex);

console.log(groups.year);  // "2026"
console.log(groups.month); // "02"
console.log(groups.day);   // "22"
Enter fullscreen mode Exit fullscreen mode

Non-Capturing Groups

Use (?:...) when you need grouping but do not need to capture:

// Match http or https without capturing the protocol
const urlRegex = /(?:https?):\/\/[\w.-]+/;
Enter fullscreen mode Exit fullscreen mode

Lookahead and Lookbehind

These match a position without consuming characters:

// Positive lookahead: match digits followed by "px"
"12px 3em 45px".match(/\d+(?=px)/g);
// Returns: ["12", "45"]

// Negative lookahead: match digits NOT followed by "px"
"12px 3em 45px".match(/\d+(?!px)/g);
// Returns: ["3"]

// Positive lookbehind: match digits preceded by "$"
"$100 200 $350".match(/(?<=\$)\d+/g);
// Returns: ["100", "350"]

// Negative lookbehind: match digits NOT preceded by "$"
"$100 200 $350".match(/(?<!\$)\d+/g);
// Returns: ["200"]
Enter fullscreen mode Exit fullscreen mode

Common Real-World Patterns

Email Validation

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

emailRegex.test("user@example.com");     // true
emailRegex.test("invalid@.com");          // false
Enter fullscreen mode Exit fullscreen mode

URL Matching

const urlRegex = /https?:\/\/(?:www\.)?[a-zA-Z0-9-]+(?:\.[a-zA-Z]{2,})+(?:\/[^\s]*)?/g;

"Visit https://example.com/path?q=1 for details".match(urlRegex);
// Returns: ["https://example.com/path?q=1"]
Enter fullscreen mode Exit fullscreen mode

IPv4 Address

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

ipRegex.test("192.168.1.1");   // true
ipRegex.test("999.999.999.999"); // false
Enter fullscreen mode Exit fullscreen mode

Phone Number (US Format)

const phoneRegex = /^(?:\+1[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/;

phoneRegex.test("(555) 123-4567");  // true
phoneRegex.test("+1-555-123-4567"); // true
Enter fullscreen mode Exit fullscreen mode

Regex in Python

import re

text = "Order #12345 placed on 2026-02-22 for $99.50"

# Find all numbers
numbers = re.findall(r'\d+\.?\d*', text)
print(numbers)  # ['12345', '2026', '02', '22', '99.50']

# Named groups with search
pattern = r'Order #(?P<order_id>\d+) placed on (?P<date>[\d-]+)'
match = re.search(pattern, text)
if match:
    print(match.group('order_id'))  # 12345
    print(match.group('date'))      # 2026-02-22

# Substitution
cleaned = re.sub(r'[^a-zA-Z\s]', '', "Hello, World! 123")
print(cleaned)  # "Hello World "
Enter fullscreen mode Exit fullscreen mode

Flags Reference

Flag JavaScript Python Meaning
Global g N/A (findall) Match all occurrences
Case-insensitive i re.IGNORECASE Ignore case
Multiline m re.MULTILINE ^ and $ match line boundaries
Dotall s re.DOTALL . matches newline
Unicode u re.UNICODE Full Unicode support

Test Your Regex Patterns

Building or debugging a regex? Use the DevToolBox Regex Tester to write, test, and visualize your regular expressions in real time with match highlighting and group extraction.

Top comments (0)