Regex Cheatsheet

A complete beginner-friendly guide to Regular Expressions. Learn how to match patterns, validate inputs, and search text with practical examples.

What is Regex?

Regex (Regular Expressions) is a powerful tool for finding and manipulating text patterns. It’s like a search language built into programming languages, editors, and command-line tools.

Example: /\d3/ matches any 3-digit number like 123.

Character Classes

Character classes let you match specific sets of characters.

[abc]   # Matches 'a', 'b', or 'c'
[^abc]  # Matches any character except 'a', 'b', or 'c'
[0-9]   # Matches digits 0 to 9
\d      # Matches any digit
\w      # Matches any word character (letters, numbers, underscore)
\s      # Matches whitespace (spaces, tabs, newlines)

Quantifiers

Quantifiers control how many times a character or group should appear.

a*     # 0 or more 'a'
a+     # 1 or more 'a'
a?     # 0 or 1 'a'
a{3}   # Exactly 3 'a'
a{2,}  # 2 or more 'a'
a{2,4} # Between 2 and 4 'a'

Anchors

Anchors match positions, not characters.

^hello   # Matches "hello" at the start
world$  # Matches "world" at the end
\bword   # Matches "word" as a whole word
\Bend   # Matches "end" inside another word

Groups & Capturing

Groups allow you to capture parts of a match or apply quantifiers to multiple characters.

(abc)     # Captures "abc"
(?:abc)  # Non-capturing group
(?<name>abc) # Named group

Alternation

Alternation allows you to match one pattern OR another.

(cat|dog)  # Matches "cat" or "dog"

Escaping

Some characters have special meanings in regex. Use \ to escape them.

\.  # Matches a literal dot
\\  # Matches a backslash

Lookaheads & Lookbehinds

Lookarounds match text based on what comes before or after, without including it in the result.

(?=abc)   # Positive lookahead
(?!abc)   # Negative lookahead
(?<=abc)  # Positive lookbehind
(?<!abc)  # Negative lookbehind

Regex Flags

Flags change how a regex behaves.

/abc/i   # Case insensitive
/abc/g   # Global (find all matches)
/abc/m   # Multiline mode
/abc/s   # Dot matches newline

Common Regex Examples

Useful patterns you can use in real life.

# Validate email
^[\w.-]+@[\w.-]+\.\w{2,}$

# Validate phone number
^\+?\d{10,15}$

# Validate URL
https?:\/\/[\w.-]+(\.[\w\.-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=.]*

# Extract domain from URL
https?:\/\/(?:www\.)?([^\/]+)

# Find hashtags
#\w+

# Password (min 8 chars, letters + numbers)
^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$

Best Practices

  • Keep regex simple and readable.
  • Use comments or break patterns into smaller parts.
  • Test regex with tools before using in production.
  • Avoid catastrophic backtracking by being specific.

How to use this page

  1. Start with character classes and quantifiers.
  2. Learn anchors and grouping for complex patterns.
  3. Practice with real-world regex examples.
  4. Experiment using regex testers online.

🚀 Explore More Free Developer Tools

Don’t stop here! Supercharge your workflow with our other powerful converters & formatters.

💡 New tools are added regularly — bookmark DevUtilsX and stay ahead!

Want to support my work?

Buy me a coffee