Core Regex Syntax & Metacharacters Reference

Regular expressions (regex) are formal pattern descriptions used by search engines, compilers, and web applications to validate, extract, and replace string data.
| Metacharacter | Meaning | Practical Example | Matches |
|---|---|---|---|
^ | Start of string / line | ^https | Strings beginning with "https" |
$ | End of string / line | \.png$ | Strings ending with ".png" |
. | Any single character (except newline) | c.t | "cat", "cot", "c9t" |
\d | Any digit (equivalent to [0-9]) | \d{4} | "2026" |
\w | Word character (alphanumeric + _) | \w+ | "user_123" |
\s | Any whitespace (space, tab, newline) | \s+ | " " |
+ | One or more occurrences | a+ | "a", "aaa" |
* | Zero or more occurrences | a* | "", "a", "aaa" |
? | Zero or one occurrence (optional) | https? | "http", "https" |
{n,m} | Between n and m repetitions | \d{2,4} | "99", "1999" |
Copy-Paste Tested Regex Patterns for Web Apps
1. Email Address Validation (RFC 5322 Simplified)

Validates standard modern emails with valid domain extensions:
^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$
2. Strong Password Validation
Requires 8+ characters, at least 1 uppercase letter, 1 lowercase letter, 1 number, and 1 special symbol:
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
3. Hex Color Codes
Matches 3-digit, 6-digit, and 8-digit (with alpha channel) hex codes:
^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$
4. International Phone Numbers (E.164 Format)

Matches standard international phone numbers starting with a plus sign:
^\+[1-9]\d{1,14}$
ReDoS Security: Preventing Catastrophic Backtracking

Regular Expression Denial of Service (ReDoS) is a critical security vulnerability. When a regular expression uses nested ambiguous quantifiers like:
(a+)+$
Evaluating a non-matching input like "aaaaaaaaaaaaaaaaaaaaaaaaaaaa!" causes the NFA regex engine to test millions of permutations. In a production Node.js or Python backend, this blocks the event loop and locks the CPU at 100%.
Rules to prevent ReDoS:
- Never nest greedy quantifiers inside other quantifiers.
- Use possessive quantifiers or atomic groups if supported by your runtime.
- Test your patterns against negative strings using our Regex Tester.

Recommended Companion Guides
Explore complementary tutorials and utilities from this topic cluster to enhance your digital workflow: