How to Debug Regular Expressions Step by Step
Master regex debugging with systematic techniques. Learn to identify pattern issues, use debugging tools, and fix common regex problems efficiently.
A comprehensive regex cheat sheet with syntax reference, examples, and best practices for matching patterns in JavaScript, Python, and more.
Regular expressions (regex or regexp) are patterns used to match character combinations in strings. They are one of the most powerful tools for text processing, validation, and extraction.
This cheat sheet covers everything from basic syntax to advanced patterns. Bookmark it for quick reference while building and debugging regular expressions.
The simplest regex patterns match literal characters. Most characters match themselves, but some have special meaning.
Quantifiers specify how many times a character or group should match.
Anchors match positions, not characters. Use them to specify where in the string a match should occur.
Shorthand character classes match common character sets.
Groups allow you to apply quantifiers to multiple characters and capture matched text for later use.
Flags modify how the regex engine interprets the pattern.
Email validation with regex is notoriously complex. Here is a practical pattern that covers most valid addresses:
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
This pattern matches a local part with common characters, followed by @, a domain name with dots, and a TLD of at least 2 letters. For production use, consider using a library or sending a verification email.
Matching URLs requires handling protocols, domains, paths, and query strings:
https?:\/\/[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=%]*
This matches http or https URLs with domain names and optional paths. For stricter validation, use the URL constructor in JavaScript.
Phone formats vary by country. Here are patterns for common formats:
Common date format patterns:
Enforce password requirements with lookaheads:
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
This requires at least 8 characters with lowercase, uppercase, digit, and special character. Adjust the character set and length based on your security requirements.
By default, quantifiers are greedy and match as much as possible. Add ? to make them lazy.
Regex can be slow with complex patterns or long strings. Follow these tips:
JavaScript provides several methods for working with regex:
Watch out for these frequent regex pitfalls:
The g (global) flag finds all matches in the string instead of stopping at the first match. Without it, methods like match() return only the first result.
Escape special characters with a backslash. Use \. to match a literal period, \* for asterisk, \[ for bracket, and so on.
.* is greedy and matches as many characters as possible. .*? is lazy and matches as few as possible. Use lazy quantifiers when extracting content between delimiters.
Regex cannot reliably match arbitrarily nested structures because it lacks the ability to count nesting depth. Use a proper parser for HTML, XML, or JSON.
Add the i flag after the pattern. In JavaScript: /pattern/i or new RegExp("pattern", "i"). This matches uppercase and lowercase letters equally.
A lookahead asserts that a pattern follows (or does not follow) the current position without including it in the match. (?=abc) is positive lookahead (followed by abc), (?!abc) is negative (not followed by abc).
Master regex debugging with systematic techniques. Learn to identify pattern issues, use debugging tools, and fix common regex problems efficiently.
Learn how to validate email addresses using regular expressions. Explore practical patterns from simple to RFC-compliant, with examples in JavaScript and other languages.
Learn how to build and debug regular expressions with live highlights, flags, and real-world examples.