Regular expressions (regex) are one of the most powerful tools in a developer's arsenal. They allow you to search, match, and manipulate text with incredible precision and efficiency.
A regular expression is a sequence of characters that defines a search pattern. Think of it as a mini programming language specifically designed for text processing.
The simplest regex pattern is just plain text:
/hello/
This matches the exact string "hello" in your text.
Match any character from a set:
[abc] - Matches 'a', 'b', or 'c'[0-9] - Matches any digit[A-Z] - Matches any uppercase letter[a-zA-Z0-9] - Matches alphanumeric characters. - Matches any single character except newline* - Matches 0 or more of the preceding element+ - Matches 1 or more of the preceding element? - Matches 0 or 1 of the preceding element^ - Matches the start of a string$ - Matches the end of a string\d - Matches any digit (0-9)\w - Matches any word character (letters, digits, underscore)\s - Matches any whitespace character/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
Validates standard email format.
/^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/
Matches: (555) 123-4567, 555-123-4567, 5551234567
/^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b/
Validates HTTP and HTTPS URLs.
/@(.+)$/
Captures everything after the @ symbol.
🔍 Test Your Regex Patterns Live →Control how many times a pattern should match:
{n} - Exactly n times{n,} - At least n times{n,m} - Between n and m timesExample: \d{3}-\d{2}-\d{4} matches Social Security numbers like 123-45-6789
Parentheses create groups that can be extracted:
/(\d{4})-(\d{2})-(\d{2})/
Matches dates like 2025-03-15 and captures year, month, and day separately.
Match patterns only if followed or preceded by something else:
(?=...) - Positive lookahead(?!...) - Negative lookahead(?<=...) - Positive lookbehind(? - Negative lookbehindRequire at least 8 characters, one uppercase, one lowercase, one digit:
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$/
/href=["']([^"']*)["']/g
/\s+/g
Replace with a single space to normalize whitespace.
/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14})$/
Matches Visa (starting with 4) and Mastercard (51-55) formats.
By default, quantifiers are greedy (match as much as possible):
/<.*>/ // Greedy - matches from first < to last >
/<.*?>/ // Lazy - matches shortest possible
To match literal special characters, escape them with backslash:
/\./ // Matches a literal dot
/\$/ // Matches a literal dollar sign
Complex regex with nested quantifiers can cause catastrophic backtracking. Keep patterns simple when possible.
Always test your regex patterns before using them in production. Our Regex Tester tool provides:
const regex = /pattern/gi; // g=global, i=case-insensitive
const match = str.match(regex);
const replaced = str.replace(regex, 'replacement');
import re
pattern = r'pattern' # Raw string
match = re.search(pattern, string)
matches = re.findall(pattern, string)
preg_match('/pattern/', $string, $matches);
preg_replace('/pattern/', 'replacement', $string);
Regular expressions are incredibly powerful once you understand the fundamentals. Practice with real-world examples, test thoroughly, and remember that readability matters - sometimes a simple solution is better than a clever regex.
Start practicing today with our free Regex Tester and master pattern matching in no time!
← Back to Blog