What Is a Regular Expression?
A regular expression (regex) is a pattern used to match, search, and manipulate text. Think of it as a powerful "find and replace" on steroids. Developers use regex for form validation, data extraction, log parsing, and much more.
Why Learn Regex?
Regex is used in virtually every programming language — JavaScript, Python, Java, Go, PHP, and more. Mastering it lets you:
- Validate user input — email, phone numbers, URLs
- Extract data from logs, HTML, or CSV files
- Search and replace complex patterns in code
- Parse text from APIs or files
- Write cleaner code with fewer lines
Basic Regex Syntax
Literal Characters
The simplest regex matches exact text. The pattern hello matches the string "hello" in any text.
Character Classes
[abc]— matches "a", "b", or "c"[0-9]— matches any digit[a-zA-Z]— matches any letter[^abc]— matches anything EXCEPT "a", "b", or "c"
Quantifiers
*— zero or more times+— one or more times?— zero or one time{3}— exactly 3 times{2,5}— between 2 and 5 times
Special Characters
.— matches any character (except newline)\d— matches any digit (same as [0-9])\w— matches any word character (letters, digits, underscore)\s— matches any whitespace^— start of string$— end of string
Practical Regex Examples
Validate an Email Address
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Match a Phone Number (US format)
^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$
Extract URLs from Text
https?:\/\/[^\s]+
Match an IP Address
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
Validate a Strong Password
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
Common Regex Mistakes
.*? instead of .* for non-greedy matching^ and $ to match the full stringTest Your Regex Online
Use our Regex Tester tool to build and test patterns in real time. See matches highlighted instantly, get explanations for your patterns, and debug with ease — all in your browser.
Regex Cheat Sheet
| Pattern | Description |
| \d | Any digit | |
| \w | Any word character | |
| \s | Any whitespace | |
| . | Any character | |
| * | Zero or more | |
| + | One or more | |
| ? | Optional | |
| ^ | Start of string | |
| $ | End of string | |
| () | Grouping | |
| \ | OR operator |
Start Practicing
The best way to learn regex is by doing. Open our Regex Tester and start experimenting with patterns. Try matching emails, URLs, and phone numbers from sample text.