Regex Cheat Sheet: Patterns, Syntax & Examples

What are regular expressions?

Regular expressions (regex or regexp) are patterns used to match character combinations in strings. They are a powerful tool for searching, validating, and manipulating text. Every major programming language supports regex: JavaScript, Python, Java, PHP, Go, and more.

Basic syntax

Character classes

Quantifiers

Anchors

Groups and alternation

Common regex patterns

Email validation

^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$

Matches most common email formats. Note: a fully RFC-compliant email regex is extremely complex; this pattern covers 99% of real-world emails.

URL validation

^https?:\/\/[\w.-]+\.[a-zA-Z]{2,}[\/\w.-]*$

Phone number (international)

^\+?[1-9]\d{6,14}$

Date format (YYYY-MM-DD)

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

IP address (IPv4)

^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$

Strong password

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

Requires: minimum 8 characters, at least one uppercase, one lowercase, one digit, and one special character.

Regex flags

Testing your regex

Always test your regular expressions before using them in production. Try our free regex tester to validate patterns in real time with instant highlighting of matches.

Performance tips

Try our free tools

Frequently Asked Questions

What is a regular expression?
A regular expression (regex) is a pattern that describes a set of strings. It's used for searching, matching, and manipulating text in programming languages and tools.
How do I test a regex pattern?
Use our free online regex tester to enter a pattern and test string. Matches are highlighted in real time, making it easy to debug and refine your expressions.
What does the g flag do in regex?
The global flag (g) tells the regex engine to find all matches in the string, not just the first one. Without it, the search stops after the first match.
How do I match an email address with regex?
A common pattern is ^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$ which matches most real-world email addresses. For strict RFC compliance, use your language's built-in email validation library.