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.t | cat, cut, c3t |
| \d | Any digit [0-9] | \d{4} | 2026, 1234 |
| \D | Any non-digit | \D+ | hello, abc |
| \w | Word char [a-zA-Z0-9_] | \w+ | hello, user_1 |
| \W | Non-word character | \W | !, @, space |
| \s | Whitespace (space, tab, newline) | \s+ | spaces, tabs |
| \S | Non-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|dog | cat 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 more | a* | a*? | matches "", "a", "aaa" |
| + | 1 or more | a+ | a+? | matches "a", "aaa" |
| ? | 0 or 1 (optional) | colou?r | — | color, 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" ❌ |
| \b | Word boundary | \bcat\b | "cat" ✅ / "cats" ❌ / "scat" ❌ |
| \B | Non-word boundary | \Bcat\B | "concatenate" ✅ / "cat" ❌ |
| \A | Absolute start of string (Python/PHP) | \Ahello | Only at very start, ignores m flag |
| \Z | Absolute end of string (Python/PHP) | world\Z | Only at very end, ignores m flag |
Groups & Back-references
| Syntax |
Meaning |
Example |
Notes |
| (abc) | Capturing group | (foo)bar\1 | Captures & 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') |
| \1 | Back-reference to group 1 | (\w+)\s\1 | Matches 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+happy | Words 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 |
| Global | Find all matches | /pat/g | findall() | preg_match_all | FindAll() |
| Case insensitive | Ignore case | /pat/i | re.IGNORECASE | /pat/i | (?i)pat |
| Multiline | ^ and $ match line starts/ends | /pat/m | re.MULTILINE | /pat/m | (?m)pat |
| Dotall | . matches newline too | /pat/s | re.DOTALL | /pat/s | (?s)pat |
| Extended | Allow whitespace & comments | — | re.VERBOSE | /pat/x | (?x)pat |
| Sticky | Match 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 email | user@example.com |
| 2 | ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ | Strict email | Full validation |
| 3 | [\w.+-]+@gmail\.com | Gmail only | user@gmail.com |
🌐 URLs & Web
| # |
Pattern |
Use case |
Matches |
| 4 | https?:\/\/[\w.-]+(?:\/[\w./?=%&-]*)? | URL extraction | http/https URLs |
| 5 | ^https?:\/\/(www\.)?[-\w]+\.[a-zA-Z]{2,}(\/\S*)?$ | URL validation | Full URL check |
| 6 | ^[a-z0-9]+(?:-[a-z0-9]+)*$ | URL slug | my-blog-post |
| 7 | [a-zA-Z0-9][a-zA-Z0-9-]{0,61}\.[a-zA-Z]{2,} | Domain name | example.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 chars | Minimum length |
| 12 | ^(?=.*[A-Z])(?=.*\d).{8,}$ | Medium strength | Uppercase + digit |
| 13 | ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&]).{8,}$ | Strong password | Lower+upper+digit+special |
🌍 IP Addresses & Networks
| # |
Pattern |
Use case |
Matches |
| 14 | ^(\d{1,3}\.){3}\d{1,3}$ | IPv4 basic | 192.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 strict | 0–255 each octet |
| 16 | ([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4} | IPv6 | 2001: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-DD | 2026-05-21 |
| 18 | ^(0[1-9]|[12]\d|3[01])\/(0[1-9]|1[0-2])\/\d{4}$ | DD/MM/YYYY | 21/05/2026 |
| 19 | ^([01]\d|2[0-3]):([0-5]\d)(:([0-5]\d))?$ | HH:MM or HH:MM:SS | 14:30 or 14:30:00 |
| 20 | \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z? | ISO 8601 | 2026-05-21T14:30:00Z |
💳 Financial & Codes
| # |
Pattern |
Use case |
Matches |
| 21 | ^4[0-9]{12}(?:[0-9]{3})?$ | Visa card | 4111111111111111 |
| 22 | ^5[1-5][0-9]{14}$ | Mastercard | 5500000000000004 |
| 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 v4 | 550e8400-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*)$ | Semver | 1.2.3, v2.0.0 |
| 29 | ^(feat|fix|docs|style|refactor|test|chore)(\(.+\))?: .+ | Conventional commit | feat(auth): add login |
| 30 | ^[A-Z][a-z]+([A-Z][a-z]+)*$ | PascalCase | MyComponent |
| 31 | ^[a-z]+([A-Z][a-z]+)*$ | camelCase | myVariable |
| 32 | ^[a-z]+(_[a-z]+)*$ | snake_case | my_variable |
📝 Text Processing
| # |
Pattern |
Use case |
Notes |
| 33 | (\w+)\s+\1 | Duplicate words | the the, is is |
| 34 | ^\s+|\s+$ | Trim whitespace | Replace with "" |
| 35 | \s{2,} | Multiple spaces | Replace with single space |
| 36 | [^\x00-\x7F] | Non-ASCII chars | Emoji, accented chars |
| 37 | ^[\u0600-\u06FF\s]+$ | Arabic text only | Arabic Unicode range |
🔑 Tokens & Keys
| # |
Pattern |
Use case |
| 38 | eyJ[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 |
| 40 | AIza[0-9A-Za-z-_]{35} | Google API key (format) |
| 41 | ghp_[A-Za-z0-9]{36} | GitHub personal access token |
📂 Files & Paths
| # |
Pattern |
Use case |
Matches |
| 42 | \.(jpg|jpeg|png|gif|webp|svg)$ | Image files | photo.jpg, icon.svg |
| 43 | \.(js|ts|jsx|tsx|mjs)$ | JS/TS files | app.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}$ | IBAN | GB29NWBK60161331926819 |
| 47 | ^[0-9]{13}$ | ISBN-13 | 9780306406157 |
| 48 | ^[A-Z]\d[A-Z] \d[A-Z]\d$ | Canadian postal code | M5V 2T6 |
| 49 | ^\d{5}(-\d{4})?$ | US ZIP code | 90210 or 90210-1234 |
| 50 | #[0-9a-fA-F]{3,8}\b | CSS 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.