⚡
Regular Expressions (Regex)
Intermediate
120 XP
50 min
Lesson Content
Regular Expressions (Regex)
Regular expressions are patterns used to match character combinations in strings. They're powerful tools for text processing and validation.
Creating Regex
// Literal notation
const pattern = /hello/;
// Constructor
const pattern2 = new RegExp('hello');
// With flags
const pattern3 = /hello/i; // case-insensitive
const pattern4 = /hello/g; // global (find all)Common Patterns
// Digits
/\d/ // Single digit
/\d+/ // One or more digits
/\d{3}/ // Exactly 3 digits
// Letters
/[a-z]/ // Lowercase letter
/[A-Z]/ // Uppercase letter
/[a-zA-Z]/ // Any letter
// Special characters
/./ // Any character except newline
/\w/ // Word character (letter, digit, underscore)
/\s/ // Whitespace
/^/ // Start of string
/$/ // End of stringRegex Methods
const text = 'Hello World';
const pattern = /hello/i;
// test() - Returns true/false
pattern.test(text); // true
// match() - Returns matches
text.match(/o/g); // ['o', 'o']
// search() - Returns index
text.search(/World/); // 6
// replace() - Replace matches
text.replace(/World/, 'JavaScript'); // 'Hello JavaScript'
// split() - Split by pattern
text.split(/\s/); // ['Hello', 'World']Common Use Cases
// Email validation
/^[^\s@]+@[^\s@]+\.[^\s@]+$/
// Phone number
/^\d{3}-\d{3}-\d{4}$/
// Password (8+ chars, 1 uppercase, 1 lowercase, 1 number)
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/Example Code
Use regular expressions to match and manipulate text
// Basic matching
const text = 'Hello World 123';
// Test if contains digits
const hasDigits = /\d/.test(text);
console.log('Has digits:', hasDigits);
// Find all digits
const digits = text.match(/\d+/g);
console.log('Digits:', digits);
// Replace digits
const noDigits = text.replace(/\d+/g, 'NUM');
console.log('No digits:', noDigits);
// Email validation
function isValidEmail(email) {
const pattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return pattern.test(email);
}
console.log('Valid email:', isValidEmail('user@example.com'));
console.log('Invalid email:', isValidEmail('invalid-email'));
// Extract phone numbers
const phoneText = 'Call 555-1234 or 555-5678';
const phones = phoneText.match(/\d{3}-\d{4}/g);
console.log('Phones:', phones);
// Split by multiple delimiters
const data = 'apple,banana;orange:grape';
const fruits = data.split(/[,;:]/);
console.log('Fruits:', fruits);
// Word boundaries
const sentence = 'The cat sat on the mat';
const wordCat = /\bcat\b/;
console.log('Found cat:', wordCat.test(sentence));
// Case-insensitive search
const mixed = 'Hello HELLO hello';
const matches = mixed.match(/hello/gi);
console.log('Matches:', matches);Expected Output:
Has digits: true Digits: [123] No digits: Hello World NUM Valid email: true Invalid email: false Phones: [555-1234,555-5678] Fruits: [apple,banana,orange,grape] Found cat: true Matches: [Hello,HELLO,hello]
Study Tips
- •Read the theory content thoroughly before practicing
- •Review the example code to understand key concepts
- •Proceed to the Practice tab when you're ready to code