Regex Cheat Sheet for Developers (with Live Tester)

Regex Cheat Sheet for Developers (with Live Tester)

Regular expressions (or Regex) are one of the most powerful tools in a developer's arsenal. They allow you to search, match, and manipulate text with incredible precision. But let's be honest: Regex can also be one of the most confusing and frustrating parts of coding.

In this guide, we’ve put together a comprehensive Regex cheat sheet and 10 practical examples to help you master regular expressions and use a regex tester online to debug your patterns.

Why Learn Regex?

Regex is everywhere. Whether you're validating a form field in JavaScript, searching through logs with grep, or renaming hundreds of files in your IDE, Regex is the tool that automates the heavy lifting. Once you understand the basic syntax, you can perform complex text operations in seconds that would otherwise take dozens of lines of code.

Essential Regex Syntax Cheat Sheet

Here are the most common characters and sequences you'll use in every Regex pattern.

CategoryCharacterDescription
Anchors^Start of a string
$End of a string
Quantifiers*0 or more occurrences
+1 or more occurrences
?0 or 1 occurrence
{n}Exactly n occurrences
{n,}n or more occurrences
{n,m}Between n and m occurrences
Character Classes\dAny digit (0-9)
\wAny alphanumeric character (a-zA-Z0-9_)
\sAny whitespace (space, tab, newline)
.Any single character (except newline)
[abc]Any one of the characters a, b, or c
[^abc]Any one character except a, b, or c
Groups(abc)Capture group
(?:abc)Non-capturing group
(a|b)Matches a OR b

10 Practical Regex Examples

Instead of just looking at theory, let's see some regular expression examples you can use in your daily work.

1. Email Address Validation

A basic email validation pattern: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

2. URL Validation

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

3. IPv4 Address

Matches a standard IP address: ^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$

4. Date (YYYY-MM-DD)

Useful for date inputs: ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$

5. Strong Password

Requires at least one uppercase, one lowercase, one digit, and one special character (8+ chars): ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$

6. HTML Tags

Finds common HTML tags: <([a-z]+)>(.*?)<\/\1>

7. Phone Number (US)

Matches (123) 456-7890: ^\(\d{3}\)\s\d{3}-\d{4}$

8. Hex Color Code

Matches #FFF or #FFFFFF: ^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$

9. Slug Format

Matches lowercase-with-dashes: ^[a-z0-9]+(?:-[a-z0-9]+)*$

10. Extracting domain name from email

Using a capture group: @([a-zA-Z0-9.-]+)$

How to Debug Your Regex Patterns

If your Regex isn't working as expected, don't bang your head against the wall. Use a Regex tester online.

  1. Paste your text: Add a large block of text that you want to search through.
  2. Type your Regex: As you type, the tool will highlight any matches in real-time.
  3. Check flags: Use the g (global), i (case-insensitive), and m (multi-line) flags as needed.
  4. Iterate: Refine your pattern until it matches exactly what you want.

Conclusion

Mastering Regex is a journey, not a destination. Even the most experienced developers keep a Regex cheat sheet nearby. The key is to start with simple patterns and gradually build up to more complex ones as your needs grow.

Try it free at ToolsForCode → Regex Tester