Short answer: most everyday validation needs the same twenty or so regular expressions: phone numbers, URLs, dates, times, passwords, emails, IP addresses and a few text-cleanup patterns. Below is each one in JavaScript syntax, tested against the samples shown, with its weak spot. If your case is different, describe it in plain English and let our free AI Regex Generator write and test a pattern for you.
Every pattern here was run in Node.js against the strings in its "Matches" and "Does not match" columns. Patterns that start with ^ and end with $ check a whole value (a form field). Patterns with the g flag find matches inside longer text.
How to use these patterns in JavaScript
Use test() to validate a value, match() or matchAll() to pull matches out of text, and replace() to clean text up:
const zip = /^\d{5}(?:-\d{4})?$/;
zip.test("90210"); // true
zip.test("9021"); // false
const text = "Docs: https://example.com/a and http://test.org/b?x=1";
text.match(/https?:\/\/[^\s<>"']+/g);
// ["https://example.com/a", "http://test.org/b?x=1"]
"is is a typo".replace(/\b(\w+)\s+\1\b/gi, "$1"); // "is a typo"Do not put the
gflag on a regex you reuse withtest(). Withg, the regex rememberslastIndexbetween calls, so the same input can return true and then false.
Regex for phone numbers
| Pattern | Matches | Does not match | Caveat |
|---|---|---|---|
/^(?:\+?1[-. ]?)?(?:\(\d{3}\)|\d{3})[-. ]?\d{3}[-. ]?\d{4}$/ | (555) 123-4567 555-123-4567 +1 555.123.4567 5551234567 | 555-1234 (555 123-4567 +44 20 7946 0958 | US/Canada (NANP) format only. It checks the shape, not whether the area code exists. |
/^\+[1-9]\d{1,14}$/ | +14155552671 +442079460958 | 14155552671 +0123 +1 415 555 2671 | E.164 international format: a plus sign and up to 15 digits, no spaces. Strip spaces and dashes before testing. |
Phone formats vary by country, so a single "international phone regex" either accepts junk or rejects real numbers. For anything beyond one country, normalize the input (remove spaces, dashes and brackets) and check the E.164 shape, or use a library such as libphonenumber.
Regex for URLs and the web
| Pattern | Matches | Does not match | Caveat |
|---|---|---|---|
/^https?:\/\/(?:[a-z0-9-]+\.)+[a-z]{2,}(?::\d{2,5})?(?:[/?#]\S*)?$/i | https://example.com http://sub.example.co.uk:8080/path?q=1#top | ftp://example.com https://localhost example.com https://exa mple.com | Validates a whole http(s) URL with a domain name. Rejects localhost and IP addresses. For real validation, new URL() is stricter. |
/https?:\/\/[^\s<>"']+/g | see https://example.com/page?x=1 now | no link here www.example.com | Finds links in text. It keeps trailing punctuation, so "https://example.com." includes the dot. |
/^[a-z0-9]+(?:-[a-z0-9]+)*$/ | my-blog-post post2 | My-Post double--dash -start | URL slug: lowercase letters, digits and single hyphens. |
/^(?:(?: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)$/ | 192.168.1.1 255.255.255.255 8.8.8.8 | 256.1.1.1 192.168.01.1 1.2.3 | IPv4 with each part 0 to 255 and no leading zeros. Does not cover IPv6. |
/^#(?:[0-9a-fA-F]{3}){1,2}$/ | #fff #1A2b3C | fff #ffff #12345g | CSS hex color, 3 or 6 digits. Add {3,4} and {6,8} variants if you need alpha (#RRGGBBAA). |
If you work with server rewrites, the same syntax shows up in Apache RewriteRule patterns; our guide to .htaccess redirect and rewrite rules has worked examples.
Regex for dates and times
| Pattern | Matches | Does not match | Caveat |
|---|---|---|---|
/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/ | 2026-11-17 1999-01-31 | 2026-13-01 2026-1-5 17-11-2026 | ISO date (YYYY-MM-DD). It still accepts 2026-02-31, because a regex does not know month lengths. |
/^(?:0[1-9]|1[0-2])\/(?:0[1-9]|[12]\d|3[01])\/\d{4}$/ | 11/17/2026 01/01/2000 | 13/01/2026 1/5/2026 2026/11/17 | US date (MM/DD/YYYY) with two-digit month and day. Same month-length caveat. |
/^(?:[01]\d|2[0-3]):[0-5]\d$/ | 00:00 09:30 23:59 | 24:00 9:30 12:60 | 24-hour time HH:MM. Add (?::[0-5]\d)? for optional seconds. |
/^(?:0?[1-9]|1[0-2]):[0-5]\d ?[AaPp][Mm]$/ | 9:30 AM 12:05pm 07:45 PM | 13:00 PM 0:30 AM 9:30 | 12-hour time with AM/PM. |
To reject impossible dates such as February 31, check the value after the regex: parse it and confirm the day did not roll over (for example, new Date(2026, 1, 31) becomes March 3).
Regex for passwords and usernames
| Pattern | Matches | Does not match | Caveat |
|---|---|---|---|
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{8,}$/ | Passw0rd! xY7#abcd | password Password1 Pa1! | At least 8 characters with a lowercase letter, an uppercase letter, a digit and a symbol. Each (?=...) lookahead adds one rule. |
/^[a-zA-Z0-9_]{3,16}$/ | john_doe User123 | jo john-doe this_name_is_way_too_long | Username of 3 to 16 letters, digits or underscores. Not Unicode-aware. |
A caveat on password rules: the US NIST digital identity guidelines (SP 800-63B, revision 4) say services should not impose composition rules such as "one uppercase, one symbol". Instead they require a minimum length (15 characters when the password is the only factor, 8 when it is part of multi-factor login) and a check against a blocklist of common or breached passwords. If you control the policy, ^.{15,}$ plus a blocklist check follows that advice.
Regex for emails, numbers and ZIP codes
| Pattern | Matches | Does not match | Caveat |
|---|---|---|---|
/^[^\s@]+@[^\s@]+\.[^\s@]+$/ | jane.doe@example.com a+tag@mail.co.uk | jane@ jane@example jane doe@example.com | A practical email check, not RFC 5322. The only real test is sending a confirmation email. |
/^-?\d+(?:\.\d+)?$/ | 42 -3.14 0.5 | 3. .5 1,000 1e5 | Integer or decimal with a dot. |
/^\d{1,3}(?:,\d{3})*(?:\.\d+)?$/ | 1,234 1,234,567.89 999 | 1234,567 12,34 ,123 | Number with comma thousands separators (US style). |
/^\d{5}(?:-\d{4})?$/ | 90210 12345-6789 | 1234 123456 12345-678 | US ZIP and ZIP+4. Other countries need their own pattern. |
Regex for cleaning up text
| Pattern | Matches | Does not match | Caveat |
|---|---|---|---|
/#[\p{L}\p{N}_]+/gu | loving #regex and #café | no tags # alone | Finds hashtags, including accented letters, thanks to the u flag and \p{L}. |
/\.(?:jpe?g|png|gif|webp)$/i | photo.JPG img.webp | photo.jpg.exe notes.txt | File extension check on a file name. It does not prove the file type. |
/\b(\w+)\s+\1\b/gi | this is is a test The the end | this is a test | Finds doubled words. Replace with "$1" to fix them. |
Mistakes that break regex examples
- Missing anchors. Without
^and$,/\d{5}/accepts "abc123456xyz" because it finds five digits somewhere inside. - Unescaped dots. A bare
.means "any character", so/example.com/also matches "exampleXcom". Write\.for a literal dot. - Nested quantifiers. Patterns like
(a+)+$can take exponential time on long input that almost matches (catastrophic backtracking). Keep quantifiers flat. - Using regex for the whole job. Dates, URLs and emails all have rules a regex cannot express. Use the regex as a quick shape check, then parse the value properly.
- Copying a pattern from another flavor. Python, PCRE and JavaScript mostly agree, but lookbehind, named groups and Unicode classes differ in places.
Write your own pattern from a description
When none of these fit, type what you need into the regex generator, for example "UK postcodes" or "lines that start with ERROR and contain an IP address". It sends your description to our AI provider and returns one JavaScript pattern with its flags and a short plain-English explanation. Before you see it, the server tests the pattern against its own match and no-match examples and asks the AI to fix it once if any example fails; the results are shown with a check or a cross.
- Describe the pattern in a sentence, including whether you want to validate a whole value or find matches in text.
- Read the explanation and the example results. A cross means the pattern got that example wrong.
- Paste your own samples into the live tester. Matches are highlighted and capture groups are listed; you can toggle the
g,i,m,s,uandyflags. - Edit the pattern directly if needed. The tester works without the AI too, so you can also paste any pattern from this page and try it.
The tool also warns when a pattern looks prone to catastrophic backtracking. It only writes JavaScript syntax, so check flavor differences before using a pattern in Python, PHP or Java.
FAQ
What is a good regex for a phone number?
For US and Canadian numbers, ^(?:\+?1[-. ]?)?(?:\(\d{3}\)|\d{3})[-. ]?\d{3}[-. ]?\d{4}$ accepts the common formats. For international numbers, strip formatting and check the E.164 shape ^\+[1-9]\d{1,14}$.
How do I write a regex for a URL?
For http and https links with a domain, use the URL pattern in the table above. In JavaScript, new URL(value) inside a try/catch is a more reliable validator; use regex mainly to find links inside text.
Can AI write a regular expression from plain English?
Yes, and it is often a good first draft, but test it. Our AI regex tool checks its own examples before showing a result, and you should still try your real data in the tester, including strings that should fail.
Can a regex validate a date completely?
Not reliably. It can check the format and ranges such as month 01 to 12, but leap years and month lengths need code. Combine a format regex with a date parse.
Are these regex examples the same in Python?
Most work unchanged in Python's re module, but the \p{L} hashtag pattern needs the third-party regex module, and flags are passed differently (re.I instead of /i).
Bookmark this page for the common cases, and when you need something specific, describe it to the free AI regex generator and test the result before you ship it.



