jeudi 21 mai 2026

The Complete Regex Cheat Sheet 2026

Regex is one of those things every developer uses but few feel truly confident with. You write a pattern, it works, you move on — and then three months later you need something slightly different and you're back on Stack Overflow searching the same concepts again.

This cheat sheet is designed to end that cycle. It covers everything from basic syntax to advanced lookaheads, with real examples in JavaScript, Python, PHP, and Go — and a free online tester so you can verify every pattern before using it in production.

Test any pattern from this cheat sheet instantly

YouKip Regex Tester — 8 languages, client-side, free. Open the tester →

Basic Syntax & Metacharacters

Token Meaning Example pattern Matches
.Any character (except newline)c.tcat, cut, c3t
\dAny digit [0-9]\d{4}2026, 1234
\DAny non-digit\D+hello, abc
\wWord char [a-zA-Z0-9_]\w+hello, user_1
\WNon-word character\W!, @, space
\sWhitespace (space, tab, newline)\s+spaces, tabs
\SNon-whitespace\S+any word
[abc]Character class — a, b, or c[aeiou]any vowel
[^abc]Negated class — not a, b, or c[^0-9]any non-digit
[a-z]Character range[a-zA-Z]any letter
|Alternation (OR)cat|dogcat or dog
\Escape special character\.literal dot

Quantifiers

Quantifiers control how many times a token can repeat. By default they are greedy — they match as much as possible. Add ? after to make them lazy (match as little as possible).

Quantifier Meaning Greedy Lazy Example
*0 or morea*a*?matches "", "a", "aaa"
+1 or morea+a+?matches "a", "aaa"
?0 or 1 (optional)colou?rcolor, colour
{n}Exactly n times\d{4}\d{4}?2026, 1234
{n,}n or more times\w{3,}\w{3,}?word with 3+ chars
{n,m}Between n and m times\d{2,4}\d{2,4}?12, 123, 1234

Anchors & Boundaries

Anchor Meaning Example Matches
^Start of string (or line with m flag)^hello"hello world" ✅ / "say hello" ❌
$End of string (or line with m flag)world$"hello world" ✅ / "world tour" ❌
\bWord boundary\bcat\b"cat" ✅ / "cats" ❌ / "scat" ❌
\BNon-word boundary\Bcat\B"concatenate" ✅ / "cat" ❌
\AAbsolute start of string (Python/PHP)\AhelloOnly at very start, ignores m flag
\ZAbsolute end of string (Python/PHP)world\ZOnly at very end, ignores m flag

Groups & Back-references

Syntax Meaning Example Notes
(abc)Capturing group(foo)bar\1Captures & backreferences with \1
(?:abc)Non-capturing group(?:foo)+Groups without capturing — faster
(?<name>)Named group (JS/PHP/Go)(?<year>\d{4})Access by name: match.groups.year
(?P<name>)Named group (Python)(?P<year>\d{4})Access: match.group('year')
\1Back-reference to group 1(\w+)\s\1Matches repeated words: "the the"
(?<=abc)Positive lookbehind(?<=\$)\d+Number after $ sign

Lookahead & Lookbehind

Lookarounds match a position without consuming characters — they're assertions, not matches. One of the most powerful and most misunderstood features in regex.

Type Syntax Example What it matches
Positive lookahead(?=abc)\w+(?=\.js)Word before ".js" — "index" in "index.js"
Negative lookahead(?!abc)\d+(?!px)Number NOT followed by "px"
Positive lookbehind(?<=abc)(?<=\$)\d+Number preceded by "$" — "100" in "$100"
Negative lookbehind(?<!abc)(?<!un)\w+happyWords ending in "happy" NOT preceded by "un"
⚠️ Lookahead/lookbehind support: All modern languages support lookahead. Lookbehind is supported in Python, PHP, and Java. JavaScript supports fixed-length lookbehind (ES2018+). Go does NOT support lookaround — use named groups instead.

Flags by Language

Flag Meaning JavaScript Python PHP Go
GlobalFind all matches/pat/gfindall()preg_match_allFindAll()
Case insensitiveIgnore case/pat/ire.IGNORECASE/pat/i(?i)pat
Multiline^ and $ match line starts/ends/pat/mre.MULTILINE/pat/m(?m)pat
Dotall. matches newline too/pat/sre.DOTALL/pat/s(?s)pat
ExtendedAllow whitespace & commentsre.VERBOSE/pat/x(?x)pat
StickyMatch only at lastIndex position/pat/y/pat/A

50 Real-World Regex Patterns

Copy-paste ready. All tested in YouKip Regex Tester across JavaScript, Python, and PHP.

📧 Email & Communication

# Pattern Use case Matches
1[\w.+-]+@[\w-]+\.[\w.]+Basic emailuser@example.com
2^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$Strict emailFull validation
3[\w.+-]+@gmail\.comGmail onlyuser@gmail.com

🌐 URLs & Web

# Pattern Use case Matches
4https?:\/\/[\w.-]+(?:\/[\w./?=%&-]*)?URL extractionhttp/https URLs
5^https?:\/\/(www\.)?[-\w]+\.[a-zA-Z]{2,}(\/\S*)?$URL validationFull URL check
6^[a-z0-9]+(?:-[a-z0-9]+)*$URL slugmy-blog-post
7[a-zA-Z0-9][a-zA-Z0-9-]{0,61}\.[a-zA-Z]{2,}Domain nameexample.com

📱 Phone Numbers

# Pattern Use case Example match
8^\+?[1-9]\d{6,14}$International phone+12125551234
9^(\+212|0)([ \-]?\d){9}$Moroccan phone+212612345678
10^(\(?\d{3}\)?[\s.-]?)?\d{3}[\s.-]?\d{4}$US phone(212) 555-1234

🔐 Passwords & Security

# Pattern Use case Requires
11^.{8,}$Min 8 charsMinimum length
12^(?=.*[A-Z])(?=.*\d).{8,}$Medium strengthUppercase + digit
13^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&]).{8,}$Strong passwordLower+upper+digit+special

🌍 IP Addresses & Networks

# Pattern Use case Matches
14^(\d{1,3}\.){3}\d{1,3}$IPv4 basic192.168.1.1
15^(25[0-5]|2[0-4]\d|[01]?\d\d?)(\.(25[0-5]|2[0-4]\d|[01]?\d\d?)){3}$IPv4 strict0–255 each octet
16([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}IPv62001:0db8::1

📅 Dates & Times

# Pattern Format Example
17^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$YYYY-MM-DD2026-05-21
18^(0[1-9]|[12]\d|3[01])\/(0[1-9]|1[0-2])\/\d{4}$DD/MM/YYYY21/05/2026
19^([01]\d|2[0-3]):([0-5]\d)(:([0-5]\d))?$HH:MM or HH:MM:SS14:30 or 14:30:00
20\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z?ISO 86012026-05-21T14:30:00Z

💳 Financial & Codes

# Pattern Use case Matches
21^4[0-9]{12}(?:[0-9]{3})?$Visa card4111111111111111
22^5[1-5][0-9]{14}$Mastercard5500000000000004
23^\d{1,3}(\.\d{3})*(,\d{2})?$Currency (EU format)1.234,56
24^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$Hex color#4f7df0 or #fff
25^[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}$UUID v4550e8400-e29b-41d4-a716-446655440000

💻 Code & Development

# Pattern Use case Matches
26\/\*[\s\S]*?\*\/|\/\/[^\n]*Strip JS comments// and /* */ comments
27<[^>]+>Strip HTML tags<div>, <img src="...">
28^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$Semver1.2.3, v2.0.0
29^(feat|fix|docs|style|refactor|test|chore)(\(.+\))?: .+Conventional commitfeat(auth): add login
30^[A-Z][a-z]+([A-Z][a-z]+)*$PascalCaseMyComponent
31^[a-z]+([A-Z][a-z]+)*$camelCasemyVariable
32^[a-z]+(_[a-z]+)*$snake_casemy_variable

📝 Text Processing

# Pattern Use case Notes
33(\w+)\s+\1Duplicate wordsthe the, is is
34^\s+|\s+$Trim whitespaceReplace with ""
35\s{2,}Multiple spacesReplace with single space
36[^\x00-\x7F]Non-ASCII charsEmoji, accented chars
37^[\u0600-\u06FF\s]+$Arabic text onlyArabic Unicode range

🔑 Tokens & Keys

# Pattern Use case
38eyJ[A-Za-z0-9-_=]+\.eyJ[A-Za-z0-9-_=]+\.[A-Za-z0-9-_.+/=]*JWT token detection
39^[A-Za-z0-9+/]{40,}={0,2}$Base64 string
40AIza[0-9A-Za-z-_]{35}Google API key (format)
41ghp_[A-Za-z0-9]{36}GitHub personal access token

📂 Files & Paths

# Pattern Use case Matches
42\.(jpg|jpeg|png|gif|webp|svg)$Image filesphoto.jpg, icon.svg
43\.(js|ts|jsx|tsx|mjs)$JS/TS filesapp.js, Component.tsx
44^\/([a-z0-9-]+\/)*[a-z0-9-]+(\.[a-z]+)?$Unix file path/var/log/app.log

🧪 Miscellaneous

# Pattern Use case Matches
45^[A-Z]{2,3}$Country code (ISO)MA, US, GBR
46^[A-Z]{3}[0-9]{6}[A-Z]{1}[0-9]{2}[A-Z]{1}[0-9]{3}[A-Z]{2}$IBANGB29NWBK60161331926819
47^[0-9]{13}$ISBN-139780306406157
48^[A-Z]\d[A-Z] \d[A-Z]\d$Canadian postal codeM5V 2T6
49^\d{5}(-\d{4})?$US ZIP code90210 or 90210-1234
50#[0-9a-fA-F]{3,8}\bCSS color (3–8 chars)#fff, #ff0000, #ff000080

Language Syntax Comparison

Same task — find all emails in a string — in four languages:

JavaScript

// Find all emails in a string
const text = "Contact alice@example.com or bob@test.org";
const emails = text.match(/[\w.+-]+@[\w-]+\.[\w.]+/g);
// → ["alice@example.com", "bob@test.org"]

Python

import re
text = "Contact alice@example.com or bob@test.org"
emails = re.findall(r'[\w.+-]+@[\w-]+\.[\w.]+', text)
# → ['alice@example.com', 'bob@test.org']

PHP

$text = "Contact alice@example.com or bob@test.org";
preg_match_all('/[\w.+-]+@[\w-]+\.[\w.]+/', $text, $matches);
// $matches[0] → ['alice@example.com', 'bob@test.org']

Go

import "regexp"
text := "Contact alice@example.com or bob@test.org"
re := regexp.MustCompile(`[\w.+-]+@[\w-]+\.[\w.]+`)
emails := re.FindAllString(text, -1)
// → ["alice@example.com", "bob@test.org"]

⚡ Test Any Pattern From This Cheat Sheet

YouKip Regex Tester Ultra — paste any pattern and test it across JavaScript, Python, PHP, Go, Java, Ruby, Rust, and C# simultaneously. 100% client-side, free, no account.

Open Regex Tester →
🎁

Get this cheat sheet as a PDF

Printable, dark-mode PDF version of all 50 patterns + full syntax reference. Free.

⬇️ Download Free PDF

Last updated: May 2026. All patterns tested in YouKip Regex Tester Ultra across JavaScript, Python, PHP, and Go. Pattern accuracy may vary for edge cases — always test with real data before deploying to production.