← Til baka

Regex Cheat Sheet 2026: The Only Reference You Need

· 5 mín lestrartími · Almennt
# Regex Cheat Sheet 2026: The Only Reference You Need Regular expressions are the Swiss Army knife of text processing. Whether you're validating input, extracting data, or doing search-and-replace, this cheat sheet has you covered. **[Try these patterns live →](https://regex.pandan.is)** ## Basic Characters | Pattern | Matches | Example | |---------|---------|--------| | `.` | Any character except newline | `h.t` matches `hat`, `hot`, `hit` | | `\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_world` | | `\W` | Non-word character | `\W` matches `@`, `#`, ` ` | | `\s` | Whitespace (space, tab, newline) | `\s+` matches spaces | | `\S` | Non-whitespace | `\S+` matches words | ## Anchors | Pattern | Matches | |---------|---------| | `^` | Start of string/line | | `$` | End of string/line | | `\b` | Word boundary | | `\B` | Not a word boundary | ## Quantifiers | Pattern | Meaning | |---------|---------| | `*` | 0 or more | | `+` | 1 or more | | `?` | 0 or 1 (optional) | | `{n}` | Exactly n times | | `{n,}` | n or more times | | `{n,m}` | Between n and m times | | `*?` | 0 or more (lazy) | | `+?` | 1 or more (lazy) | ## Character Classes | Pattern | Matches | |---------|---------| | `[abc]` | a, b, or c | | `[^abc]` | Not a, b, or c | | `[a-z]` | Any lowercase letter | | `[A-Z]` | Any uppercase letter | | `[0-9]` | Any digit | | `[a-zA-Z0-9]` | Any alphanumeric | ## Groups & Alternation | Pattern | Meaning | |---------|---------| | `(abc)` | Capture group | | `(?:abc)` | Non-capturing group | | `(?abc)` | Named capture group | | `a\|b` | a or b (alternation) | | `\1` | Backreference to group 1 | ## Lookahead & Lookbehind | Pattern | Meaning | |---------|---------| | `(?=abc)` | Positive lookahead | | `(?!abc)` | Negative lookahead | | `(?<=abc)` | Positive lookbehind | | `(?]*)?\/?> ``` ### Hex Color Code ``` ^#(?:[0-9a-fA-F]{3}){1,2}$ ``` ### Slug (URL-friendly string) ``` ^[a-z0-9]+(?:-[a-z0-9]+)*$ ``` ## JavaScript Examples ### Test a pattern ```javascript const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; console.log(emailRegex.test('user@example.com')); // true ``` ### Extract matches ```javascript const text = 'Call 555-1234 or 555-5678'; const phones = text.match(/\d{3}-\d{4}/g); // ['555-1234', '555-5678'] ``` ### Named groups ```javascript const dateStr = '2026-03-26'; const { year, month, day } = dateStr.match( /(?\d{4})-(?\d{2})-(?\d{2})/ ).groups; ``` ### Replace with callback ```javascript const result = 'hello world'.replace( /\b(\w)/g, (match, letter) => letter.toUpperCase() ); // 'Hello World' ``` ## Python Examples ```python import re # Find all emails in text emails = re.findall(r'[\w.+-]+@[\w-]+\.[\w.]+', text) # Replace digits with # cleaned = re.sub(r'\d', '#', 'card: 4111-1111-1111-1111') # 'card: ####-####-####-####' # Named groups m = re.match(r'(?P\w+) (?P\w+)', 'John Doe') print(m.group('first')) # John ``` ## Regex Flags | Flag | Meaning | |------|---------| | `i` | Case insensitive | | `g` | Global (find all) | | `m` | Multiline (^ and $ match line boundaries) | | `s` | Dotall (. matches newline) | | `u` | Unicode | | `x` | Extended (ignore whitespace, allow comments) | ## Performance Tips 1. **Be specific.** `[a-z]+` is faster than `.*` 2. **Anchor when possible.** `^prefix` is much faster than searching everywhere 3. **Avoid catastrophic backtracking.** Nested quantifiers like `(a+)+` can hang 4. **Use non-capturing groups** `(?:...)` when you don't need the match 5. **Compile once, use many.** Don't recreate regex objects in loops ## Test Your Patterns Don't memorize — experiment! Use our **[free regex tester](https://regex.pandan.is)** with real-time highlighting, match info, and flag support. --- *More developer tools at [pandan.is](https://dev.pandan.is): [Screenshot API](https://api.pandan.is) · [QR Code API](https://qr.pandan.is) · [JWT Decoder](https://jwt.pandan.is) · [Cron Builder](https://cron.pandan.is)*