Most regex questions come down to the same fifteen or so patterns. The trouble is that the version you find in a hurry often works on the happy path and fails on the input your users actually send. Below are the patterns we reach for most, each explained piece by piece, with examples of what should and shouldn't match. Every example on this page is tested against its pattern.
The syntax is JavaScript's, which is close to what Python, Java, PHP (PCRE), Go and .NET use for everything shown here. To experiment, paste any pattern into the Regex Tester.
Five things to know first
- Anchor validation patterns. Without
^and$,\d{5}happily matches the 12345 inside "abc123456". - Escape the dot.
.means any character;\.means a literal dot. Unescaped dots in domain and IP patterns are a classic bug. - Prefer specific classes to
.*.[^"]*("anything but a quote") is faster and more predictable than.*?. - Use
(?:…)when you don't need the capture. It keeps your group numbering simple. - Validate format, not meaning. Regex can say a string looks like a date or an email. Whether it's a real date or a deliverable address is a job for code.
The patterns
1. Email address (pragmatic)
/^[^\s@]+@[^\s@]+\.[^\s@]+$/Something without spaces or @, an @, then a domain containing at least one dot.
- Matches:
"ada@example.com","first.last+news@sub.example.co.uk" - Doesn't match:
"ada@example","ada @example.com","ada@@example.com"
Watch out: Deliberately loose. The full RFC 5322 grammar can't be expressed sanely as a regex, and strict patterns reject real addresses. The only true test is sending a confirmation email.
2. URL (http or https)
/^https?:\/\/[^\s/$.?#][^\s]*$/iThe scheme, then at least one character that can start a host name, then anything up to the first whitespace.
- Matches:
"https://example.com","http://localhost:3000/path?q=1#top" - Doesn't match:
"example.com","https://","ftp://example.com"
Watch out: Good for spotting links in text. To validate a URL properly in JavaScript, use new URL(value) in a try/catch.
3. ISO 8601 date (YYYY-MM-DD)
/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/Four digits, a month from 01 to 12, a day from 01 to 31.
- Matches:
"2026-10-16","1999-01-31" - Doesn't match:
"2026-13-01","2026-1-5","16/10/2026"
Watch out: It accepts 2026-02-31. Check the calendar in code; regex is the wrong tool for leap years.
4. 24-hour time (HH:MM)
/^([01]\d|2[0-3]):[0-5]\d$/Hours 00–19 or 20–23, a colon, minutes 00–59.
- Matches:
"00:00","09:45","23:59" - Doesn't match:
"24:00","9:45","12:60"
5. Hex colour
/^#(?:[0-9a-f]{3}){1,2}$/iA # followed by exactly 3 or 6 hex digits. The group of three is repeated once or twice.
- Matches:
"#fff","#6366F1" - Doesn't match:
"#ffff","6366f1","#ggg"
6. IPv4 address
/^((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/Four numbers from 0 to 255 separated by dots. Each octet is spelled out as 250–255, 200–249, 100–199 or 0–99.
- Matches:
"192.168.1.1","10.0.0.255","8.8.8.8" - Doesn't match:
"256.1.1.1","1.2.3","01.2.3.4"
Watch out: The naive \d{1,3}(\.\d{1,3}){3} accepts 999.999.999.999.
7. International phone number (E.164)
/^\+[1-9]\d{6,14}$/A plus sign, a country code that doesn't start with 0, and up to 15 digits in total.
- Matches:
"+14155550123","+447911123456" - Doesn't match:
"0044 7911 123456","+0123456789","+1 415 555 0123"
Watch out: Strip spaces, dashes and brackets before testing. For real validation, use a library such as libphonenumber.
8. URL slug
/^[a-z0-9]+(?:-[a-z0-9]+)*$/Lowercase words and digits joined by single hyphens, with no hyphen at either end.
- Matches:
"jpg-vs-png","uuid-v7","a" - Doesn't match:
"-leading","double--dash","Upper-Case"
9. UUID
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i8-4-4-4-12 hex digits, with a version digit (1–8) and a variant digit (8, 9, a or b) in the right places.
- Matches:
"0192d4e8-7b3a-7c41-9f2e-5a8b1c3d4e6f","2F1C8A6E-93B0-4D5E-A7C2-6B1E0F9D8C34" - Doesn't match:
"0192d4e8-7b3a-0c41-9f2e-5a8b1c3d4e6f","0192d4e87b3a7c419f2e5a8b1c3d4e6f"
10. Password rule: 12+ chars, upper, lower and digit
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{12,}$/Three lookaheads each check that a character type appears somewhere, without consuming anything; then .{12,} checks the length.
- Matches:
"correctHorse42battery","Abcdefghijk1" - Doesn't match:
"Short1A","alllowercase123","NODIGITSHEREatall"
Watch out: Lookaheads are the standard way to express 'must contain X and Y and Z' in one pattern.
11. US ZIP code
/^\d{5}(?:-\d{4})?$/Five digits, optionally followed by a hyphen and four more (ZIP+4).
- Matches:
"94103","94103-1234" - Doesn't match:
"9410","94103-12","94103 1234"
12. Number with thousands separators
/^-?\d{1,3}(?:,\d{3})*(?:\.\d+)?$/One to three leading digits, then groups of exactly three after each comma, with optional decimals.
- Matches:
"1,234","-1,234,567.89","999" - Doesn't match:
"1,23","1234,567",",123"
Watch out: Many locales swap the roles: 1.234.567,89 in German, 1 234 567,89 in French.
13. Repeated word (the the)
/\b(\w+)\s+\1\b/giCapture a word, then require whitespace and the same word again. \1 is a backreference to the first group.
- Matches:
"this is the the problem","It It happens" - Doesn't match:
"this is the problem","the theory"
14. Collapse runs of whitespace
/\s+/gMatches any run of spaces, tabs or newlines. Replace with a single space to normalise text.
- Matches:
"two spaces","tab\there" - Doesn't match:
"nospace"
15. Leading and trailing whitespace
/^\s+|\s+$/gEither whitespace at the very start or whitespace at the very end. Replace with nothing to trim.
- Matches:
" padded","padded " - Doesn't match:
"clean text"
Watch out: In JavaScript, String.prototype.trim() does this and is clearer. The regex is useful in editors and other languages.
Performance: avoid catastrophic backtracking
Most regex engines, including JavaScript's, backtrack: when part of a pattern fails, they go back and try another way of matching the earlier parts. Normally that's quick. But patterns with nested quantifiers, like (a+)+$ or (\w+\s?)*$, give the engine an exponential number of ways to split the input. On a string that almost matches, a few dozen characters can keep a CPU busy for seconds or longer.
This is a real security issue, known as ReDoS, and it has taken down production services. To stay safe:
- Avoid a quantifier applied to a group that itself contains a quantifier, unless the parts can't overlap.
- Make alternatives mutually exclusive:
(\d+|\w+)is risky because digits are also word characters. - Limit input length before matching untrusted text.
- For server-side code on untrusted input, consider an engine with guaranteed linear time, like RE2 (used by Go's
regexppackage).
Flags worth knowing
| Flag | Effect |
|---|---|
g | Find all matches, not just the first. |
i | Case-insensitive. |
m | ^ and $ match at each line break, not just the ends of the string. |
s | . also matches newlines. |
u | Unicode mode: enables \p{L} and handles emoji as single characters. |
y | Sticky: match only at the current position. Useful for writing tokenisers. |
When not to use a regex
Regex is the wrong tool for nested structures. HTML, JSON and programming languages can't be parsed reliably with it, because they allow arbitrary nesting. Use a real parser (DOMParser, JSON.parse). It's also a poor fit for anything that needs arithmetic or a lookup, such as valid calendar dates, credit card checksums or country-specific postcodes. Keep regex for what it does best: recognising the shape of text.