📚 What You'll Learn
- • Regex syntax and basic patterns
- • Character classes and quantifiers
- • Grouping and capturing
- • Practical validation examples
- • Common regex use cases
- • Testing and debugging techniques
What Are Regular Expressions?
Regular expressions (regex) are powerful patterns used to match, search, and manipulate text. They're supported in nearly every programming language and are essential for:
- Data validation (emails, phone numbers, passwords)
- Text search and replace
- Data extraction from strings
- String parsing and splitting
- Syntax highlighting
Basic Regex Syntax
1. Literal Characters
The simplest regex matches exact characters:
hello
Matches: hello in "hello world"
2. Character Classes
Match specific sets of characters:
[aeiou]
Matches any vowel: a, e, i, o, u
[A-Z]
Matches any uppercase letter
[0-9]
Matches any digit (0-9)
3. Special Character Classes
\d
Matches any digit (same as [0-9])
\w
Matches any word character (letters, digits, underscore)
\s
Matches any whitespace character
.
Matches any character except newline
4. Quantifiers
Specify how many times a pattern should match:
a{3}
Matches exactly 3 "a" characters: aaa
a{2,4}
Matches 2 to 4 "a" characters: aa, aaa, aaaa
a+
Matches one or more "a" characters
a*
Matches zero or more "a" characters
a?
Matches zero or one "a" character
Practical Examples
Email Validation
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Validates standard email addresses
Phone Number (US Format)
^\(\d{3}\) \d{3}-\d{4}$
Matches: (123) 456-7890
URL Validation
^https?://(www\.)?[a-zA-Z0-9-]+\.[a-zA-Z]{2,}(/\S*)?$
Matches HTTP/HTTPS URLs
Password Strength
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
Requires: lowercase, uppercase, digit, special character, min 8 chars
Common Use Cases
1. Form Validation
Use regex to validate user input in web forms:
// JavaScript email validation
function isValidEmail(email) {
const regex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
return regex.test(email);
}
2. Text Search and Replace
Find and replace patterns in text:
// Replace all phone numbers with [REDACTED]
const text = "Call me at (123) 456-7890 or (987) 654-3210";
const redacted = text.replace(/\(\d{3}\) \d{3}-\d{4}/g, '[REDACTED]');
// Result: "Call me at [REDACTED] or [REDACTED]"
3. Data Extraction
Extract specific information from strings:
// Extract all hashtags from text
const text = "Learning #regex is fun! #programming #webdev";
const hashtags = text.match(/#\w+/g);
// Result: ["#regex", "#programming", "#webdev"]
Regex Flags
Flags modify regex behavior:
g- Global search (find all matches)i- Case-insensitive searchm- Multiline modes- Dot matches newlineu- Unicode mode
Testing and Debugging
Use our Regex Tester tool to:
- Test patterns against sample text
- See matches in real-time
- Experiment with different flags
- Learn from built-in examples
Best Practices
- Start simple - Build complex patterns incrementally
- Test thoroughly - Use multiple test cases
- Document patterns - Add comments for complex regex
- Consider performance - Avoid catastrophic backtracking
- Use built-in tools - Leverage language-specific regex features
Common Pitfalls
⚠️ Watch Out For:
- • Greedy vs lazy quantifiers (
*vs*?) - • Escaping special characters (
\.for literal dot) - • Anchors (
^start,$end) - • Character class ranges (
[A-z]includes special characters!)
Next Steps
Now that you understand the basics:
- Practice with our interactive Regex Tester
- Try the built-in examples in the tool
- Experiment with different patterns
- Bookmark common patterns for future reference