DEV Community

TextKit
TextKit

Posted on

Regex Cheat Sheet: 10 Patterns That Handle 90% of Real Work

I've been writing regex for years and I still look things up constantly. The problem with most cheat sheets is they list every possible syntax token without telling you which ones you'll actually use.

Below are my working references, the patterns I reach for over and over.

The six characters you need to know

\d  →  any digit (0-9)
\w  →  any word character (letter, digit, underscore)
\s  →  any whitespace (space, tab, newline)
\D  →  any NON-digit
\W  →  any NON-word character
\S  →  any NON-whitespace
Enter fullscreen mode Exit fullscreen mode

Uppercase = inverse. That's the whole pattern.

Quantifiers

+      one or more
*      zero or more
?      zero or one (optional)
{3}    exactly 3
{2,5}  between 2 and 5
{3,}   3 or more
Enter fullscreen mode Exit fullscreen mode

The * vs + distinction matters: \d* matches an empty string (zero digits is fine). \d+ requires at least one digit. When in doubt, you want +.

The 10 patterns I copy-paste the most

1. Email

[\w.-]+@[\w.-]+\.\w{2,}
Enter fullscreen mode Exit fullscreen mode

Not RFC-perfect. Doesn't need to be. Handles real-world emails.

const emails = text.match(/[\w.-]+@[\w.-]+\.\w{2,}/g);
Enter fullscreen mode Exit fullscreen mode

2. URLs

https?:\/\/[\w\-._~:\/?#\[\]@!$&'()*+,;=%]+
Enter fullscreen mode Exit fullscreen mode

The s? makes "s" optional; it catches both http and https.

const urls = text.match(/https?:\/\/[\w\-._~:\/?#\[\]@!$&'()*+,;=%]+/g);
Enter fullscreen mode Exit fullscreen mode

3. US phone numbers

\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
Enter fullscreen mode Exit fullscreen mode

Handles 123-456-7890, (123) 456-7890, 123.456.7890, and 1234567890.

4. IP addresses (IPv4)

\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
Enter fullscreen mode Exit fullscreen mode

The \b word boundaries are important. Without them you'd match numbers inside longer strings.

5. Dates (YYYY-MM-DD)

\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])
Enter fullscreen mode Exit fullscreen mode

Validates format and checks month is 01-12, day is 01-31.

6. Hex colors

#(?:[0-9a-fA-F]{3}){1,2}\b
Enter fullscreen mode Exit fullscreen mode

Matches both short #fff and long #ff00aa format.

7. Everything between double quotes

"([^"]*)"
Enter fullscreen mode Exit fullscreen mode

The capture group ([^"]*) grabs the content. [^"]* means "any character except a quote, zero or more times."

8. Whole word match

\bword\b
Enter fullscreen mode Exit fullscreen mode

\b is the word boundary anchor. \bcat\b matches "cat" but not "catch" or "concatenate".

9. Numbers with optional decimals

-?\d+\.?\d*
Enter fullscreen mode Exit fullscreen mode

Matches 42, 3.14, -7, -0.5.

10. Multiple whitespace (for cleanup)

\s{2,}
Enter fullscreen mode Exit fullscreen mode

Find two or more consecutive whitespace characters. Replace with a single space.

const clean = text.replace(/\s{2,}/g, ' ');
Enter fullscreen mode Exit fullscreen mode

The three mistakes I see constantly

1. Not escaping periods. . matches ANY character. \. matches an actual period. This one bites everyone at some point.

2. Greedy vs lazy. ".*" on the string "hello" and "world" matches "hello" and "world", basically everything from first quote to last. Use ".*?" to match shortest: "hello" and "world" separately.

3. Forgetting the g flag. Without it, you only get the first match. Add g for global.

// Only first match
'abc 123 def 456'.match(/\d+/)    // ["123"]

// All matches
'abc 123 def 456'.match(/\d+/g)   // ["123", "456"]
Enter fullscreen mode Exit fullscreen mode

Try it live

I built a regex tester with real-time match highlighting and a built-in cheat sheet. Paste a pattern, paste some text, see matches instantly. Runs in-browser, nothing stored.

Full version of this cheat sheet with lookahead/lookbehind and more examples: textkit.dev/blog/regex-cheat-sheet-beginners

Top comments (0)