What Are Regular Expressions?
Regular expressions (regex) are patterns used to match character combinations in strings. They're supported in virtually every programming language and tool — JavaScript, Python, grep, sed, VS Code find/replace, database queries. Mastering regex unlocks powerful text search, validation, extraction, and transformation capabilities.
How to Use This Regex Cheatsheet
- Identify your task — search, validate, extract, or replace.
- Find the right syntax — use the sections below to build your pattern.
- Test in a regex tester — regex101.com lets you test patterns in real time.
- Check flag requirements — most patterns need the global (
g) and sometimes multiline (m) flag. - Escape special characters —
. * + ? ^ $ { } [ ] | ( ) \are metacharacters that need escaping with\when used literally.
Regex Syntax Reference
Character Classes
. # Any character except newline
\d # Digit [0-9]
\D # Non-digit
\w # Word character [a-zA-Z0-9_]
\W # Non-word character
\s # Whitespace (space, tab, newline)
\S # Non-whitespace
[abc] # a, b, or c
[^abc] # Not a, b, or c
[a-z] # Any lowercase letter
[A-Z0-9] # Uppercase or digit
Quantifiers
* # 0 or more
+ # 1 or more
? # 0 or 1 (optional)
{3} # Exactly 3
{3,} # 3 or more
{3,6} # Between 3 and 6
*? # Lazy (non-greedy) match
Anchors and Boundaries
^ # Start of string/line
$ # End of string/line
\b # Word boundary
\B # Non-word boundary
Groups and References
(abc) # Capturing group
(?:abc) # Non-capturing group
(?<name>) # Named capturing group
\1 # Backreference to group 1
| # Alternation (or)
Lookahead and Lookbehind
(?=...) # Positive lookahead
(?!...) # Negative lookahead
(?<=...) # Positive lookbehind
(?<!...) # Negative lookbehind
Practical Regex Patterns
# Email validation (simplified)
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
# URL
https?://[\w\-]+(\.[\w\-]+)+([\w\-.,@?^=%&:/~+#]*)?
# IPv4 address
^(\d{1,3}\.){3}\d{1,3}$
# Date (YYYY-MM-DD)
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
# Hex color
^#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$
Use Cases
Form Validation
Regex validates email addresses, phone numbers, zip codes, and URLs in web forms. While complex validation requires backend logic, regex provides fast client-side feedback.
Log Parsing
Extracting IP addresses, timestamps, and error codes from log files is a core regex use case. Combine with grep or Python's re module to process thousands of lines instantly.
aiforeverthing.com — Regex tester and developer utilities. No signup required.
Frequently Asked Questions
What is the difference between greedy and lazy matching?
Greedy quantifiers (*, +) match as much as possible. Lazy quantifiers (*?, +?) match as little as possible. For example, <.+> on <a>text</a> matches the whole string; <.+?> matches just <a>.
Are regex patterns the same across all languages?
The core syntax is similar but not identical. JavaScript, Python, Java, and PHP all support PCRE-style regex with minor differences. Some features like lookbehind with variable length are supported in some engines but not others.
How do I match a literal dot or other metacharacter?
Escape it with a backslash: \. matches a literal period, \* matches a literal asterisk. In character classes [.], most metacharacters lose their special meaning.
What regex flags should I use in JavaScript?
Common flags: g (global, find all matches), i (case-insensitive), m (multiline, ^/$ match line starts/ends), s (dotAll, . matches newlines). Example: /pattern/gi.
Should I use regex to parse HTML?
No. Use an HTML parser (DOMParser in browsers, Cheerio in Node.js, BeautifulSoup in Python). HTML is not a regular language and cannot be reliably parsed with regex. For small, predictable extractions it works, but it breaks with nested elements and edge cases.
Recommended Hosting for Developers
- Hostinger — From $2.99/mo. Excellent for static sites and Node.js apps.
- DigitalOcean — $200 free credit for new accounts. Best for scalable backends.
- Namecheap — Budget-friendly shared hosting with free domain.