← Back to Blog
Quick Tip

The Ultimate Cheatsheet for Regular Expressions (Regex)

Let's be honest: the first time you look at a Regular Expression, it looks like a cat walked across a keyboard. Something like ^(?=.*[A-Z])(?=.*\d).{8,}$ looks terrifying, but it is actually one of the most elegant and powerful tools a developer can wield.

Regex is a universal language used for pattern matching and string manipulation. Whether you are validating a user's email address on a form or searching a massive database for phone numbers, Regex is the engine that does the heavy lifting. Bookmark this page, because here is your ultimate quick-reference cheatsheet.

1. The Basic Cheat Codes (Metacharacters)

These are the fundamental building blocks of any expression.

  • . : Matches any single character (except a newline).
  • \w : Matches any word character (a-z, A-Z, 0-9, and underscore).
  • \d : Matches any digit (0-9).
  • \s : Matches any whitespace character (space, tab, newline).
  • \W, \D, \S : The capitalized versions mean the exact opposite (e.g., \D matches anything that is NOT a digit).

2. Anchors & Boundaries

Anchors don't match characters; they match positions in the text.

  • ^ : Matches the start of a string.
  • $ : Matches the end of a string.
  • \b : Matches a word boundary (the invisible space between a word and a non-word).

3. Quantifiers (How Many?)

Want to match three digits in a row? Or maybe one or more letters? Use quantifiers.

  • * : Matches 0 or more of the preceding character.
  • + : Matches 1 or more of the preceding character.
  • ? : Matches 0 or 1 (makes the preceding character optional).
  • {3} : Matches exactly 3 of the preceding character.
  • {2,5} : Matches between 2 and 5 of the preceding character.

Real-World Snippets You Can Copy/Paste

Stop trying to reinvent the wheel. Here are the most common Regex patterns developers use every day:

Email Address Validation

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

Strong Password Validation

Requires at least 8 characters, at least one uppercase letter, and at least one number.

^(?=.*[A-Z])(?=.*\d)[A-Za-z\d]{8,}$

URL / Website Validation

^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$

Hex Color Code Validation

Matches standard 3-digit or 6-digit hex color codes (e.g., #FFF or #6c3aff).

^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$

Test Your Regex Live!

Regex is notoriously tricky to get right on the first try. Don't guessβ€”test! Paste your text and your Regex pattern into our live tester to see instant highlighting of your matches.

Open Live Regex Tester

Conclusion

The secret to mastering Regex is realizing that you don't need to memorize every single symbol. Keep this cheatsheet handy, understand the basic building blocks, and use a live testing tool to verify your patterns before you deploy them to production. Happy matching!

🐞 Found a bug or any issue?