Regular expressions solve many routine tasks: validating forms, extracting data from text, cleaning logs, and parsing configurations. This article features 20 ready-to-use patterns with explanations and usage examples. You can test and refine expressions in the Regex Tester on reChecker. Syntax and theory are covered in the regex cheat sheet and the complete guide.
1. Email
A basic pattern for most addresses:
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$An extended version (a simplified RFC 5322):
^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$JavaScript example:
const emailRe = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
emailRe.test('user@example.com'); // true
emailRe.test('invalid'); // false2. Phone number (Russia)
Russian phone numbers in different formats:
^(\+7|8)[\s-]?\(?[489]\d{2}\)?[\s-]?\d{3}[\s-]?\d{2}[\s-]?\d{2}$Breakdown:
(\+7|8)— country code +7 or 8[489]— carrier code (4xx, 8xx, 9xx)- The rest — 7 digits with optional separators
Capture digits for normalization:
^(\+7|8)[\s\-()]*(\d{3})[\s\-()]*(\d{3})[\s\-]*(\d{2})[\s\-]*(\d{2})$Replacement: +7$2$3$4$5 → +79991234567
3. URL
A basic pattern for HTTP/HTTPS URLs:
^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&/=]*)$Simplified:
^https?:\/\/[^\s/$.?#].[^\s]*$Extracting URLs from text:
https?:\/\/[^\s<>"{}|\\^`\[\]]+4. IPv4
Exact validation (0–255 in each octet):
^(?:(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)$Simplified (allows 000–999):
^(?:\d{1,3}\.){3}\d{1,3}$5. IPv6
A basic pattern for IPv6:
^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$Including zero compression (::):
^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:)*:([0-9a-fA-F]{1,4}:)*[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:)$6. Date (DD.MM.YYYY)
^(0[1-9]|[12]\d|3[01])\.(0[1-9]|1[0-2])\.(19|20)\d{2}$Capture groups for validation:
const dateRe = /^(0[1-9]|[12]\d|3[01])\.(0[1-9]|1[0-2])\.(19|20)\d{2}$/;
const m = '31.12.2025'.match(dateRe);
// m[1]=день, m[2]=месяц, m[3]=год7. Date (ISO 8601)
^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})?)?$Simplified (date only):
^\d{4}-\d{2}-\d{2}$8. Password (complexity)
At least 8 characters, with letters, digits, and special characters:
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*(),.?":{}|<>]).{8,}$Requirement options:
- Length only, 8+:
^.{8,}$ - Letters and digits:
^(?=.*[A-Za-z])(?=.*\d).{8,}$ - No special characters:
^[A-Za-z0-9]{8,}$
9. Slug (URL-friendly)
Latin letters, digits, and hyphens:
^[a-z0-9]+(?:-[a-z0-9]+)*$Validating an existing slug. To generate one from a string, transliterate it and replace spaces with hyphens.
10. Extracting HTML tags
Content between matching tags:
<(\w+)[^>]*>(.*?)</\1>Group 1 is the tag name; group 2 is the content. Use with the s flag if the content spans multiple lines.
Extracting attributes:
(\w+)=["']([^"']*)["']11. Parsing logs (Nginx/Apache)
Standard combined log format:
^(\S+)\s(\S+)\s(\S+)\s\[([^\]]+)\]\s"([^"]*)"\s(\d+)\s(\d+)\s"([^"]*)"\s"([^"]*)"$Groups: IP, identifier, user, date, request, status, size, referer, user-agent.
Simplified (IP, date, method, URL, status):
^(\d+\.\d+\.\d+\.\d+).*\[([^\]]+)\].*"(\w+)\s([^\s]+).*"\s(\d+)12. Extracting numbers
Integers:
-?\d+Decimals:
-?\d+(?:\.\d+)?With thousands separators (1 000 000):
-?(?:\d{1,3}(?:\s\d{3})*|\d+)(?:,\d+)?13. Removing HTML tags
<[^>]+>Replacing with an empty string removes the tags. This does not work for nested structures — use an HTML parser.
14. Finding duplicate words
\b(\w+)\s+\1\bFinds “the the,” “is is,” and so on. The i flag makes the search case-insensitive.
15. Extracting JSON fields
Simply extract a value by key:
"name"\s*:\s*"([^"]*)"Nested structures and escaped quotation marks require a JSON parser.
16. Hex color validation
^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$Supports 3- and 6-digit formats. With an alpha channel:
^#([a-fA-F0-9]{8}|[a-fA-F0-9]{6}|[a-fA-F0-9]{3})$17. Splitting CSV (simple case)
For strings without quotes or commas inside fields:
,(?=(?:[^"]*"[^"]*")*[^"]*$)The separator is a comma outside quotes. For full CSV support, use a specialized parser.
18. Finding quoted words
["']([^"']*)["']Or with escape support:
"([^"\\]*(?:\\.[^"\\]*)*)"19. Whitespace normalization
Replace multiple spaces with one:
\s+Replaces them with a single space. To remove leading and trailing whitespace, use ^\s+|\s+$ or trim() in code.
20. Extracting a domain from a URL
^https?:\/\/(?:www\.)?([^\/\s]+)Group 1 is the domain. To extract it from text:
https?:\/\/(?:www\.)?([^\/\s]+)Summary table
| Task | Pattern |
|---|---|
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ | |
| Phone (Russia) | ^(\+7|8)[\s-]?\(?[489]\d{2}\)?[\s-]?\d{3}[\s-]?\d{2}[\s-]?\d{2}$ |
| URL | ^https?:\/\/[^\s/$.?#].[^\s]*$ |
| IPv4 | ^(?:(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)$ |
| Date DD.MM.YYYY | ^(0[1-9]|[12]\d|3[01])\.(0[1-9]|1[0-2])\.(19|20)\d{2}$ |
| Password 8+ | ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$ |
| Slug | ^[a-z0-9]+(?:-[a-z0-9]+)*$ |
| Hex color | ^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$ |
| HTML tags | <[^>]+> |
| Duplicate words | \b(\w+)\s+\1\b |
When copying the patterns, replace | with | (the vertical bar used for alternation in regex).
Additional examples
Extracting a price from text
\d+(?:\s?\d{3})*(?:[.,]\d{2})?\s?(?:руб|₽|RUB)Finds “1 500 rub,” “10.99 ₽,” and “1 000 000 RUB.”
Validating a filename
Letters, digits, hyphens, underscores, and periods only:
^[\w\-\.]+$Finding @username mentions
@([a-zA-Z0-9_]{1,15})Group 1 is the username (up to 15 characters, as on Twitter).
Extracting hashtags
#([a-zA-Zа-яА-ЯёЁ0-9_]+)Whitespace normalization (multiline text)
Remove empty lines, leaving one:
\n{3,}Replace with \n\n. Or collapse all whitespace sequences into a single space:
[ \t]+Checking for a palindrome (letters only)
For short strings, ignoring case and spaces:
^(\w)\w*\1$|^(\w)(\w)\w*\3\2$|^(\w)(\w)(\w)\w*\5\4\3$A general-purpose palindrome check using regex is practically impossible — use a loop in code.
Time validation (HH:MM)
^([01]?\d|2[0-3]):([0-5]\d)$Groups: hours and minutes. Supports the 24-hour format.
Extracting a version from a string
v?(\d+(?:\.\d+){0,2})Finds “1.2.3,” “v2.0,” and “10.”
Bank card masking
Show only the last 4 digits:
'4111111111111111'.replace(/\d(?=\d{4})/g, '*');
// '************1111'Splitting camelCase
([a-z])([A-Z])Replace with $1 $2 — “camelCase” → “camel Case.”
Usage recommendations
- Validation vs. parsing — for strict validation, use
^and$. For searching within text, omit them. - Language — check support in your engine (lookbehind in older JS versions, Unicode support across versions).
- Performance — avoid nested quantifiers and overly broad patterns on large texts.
- Readability — for complex expressions, use the
xflag (where available) and comments.
Related materials
For syntax and all constructs, see the regex cheat sheet. For detailed theory, see the complete guide. You can test expressions in the Regex Tester.