Skip to content

20 Regex Examples You'll Actually Use: Phone, URL, Date and Password

Twenty common regex patterns in JavaScript syntax, each tested against real samples, with what it matches, what it rejects and where it falls short.

7 min readBy Admin

20 Regex Examples You'll Actually Use: Phone, URL, Date and Password

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 g flag on a regex you reuse with test(). With g, the regex remembers lastIndex between calls, so the same input can return true and then false.

Regex for phone numbers

PatternMatchesDoes not matchCaveat
/^(?:\+?1[-. ]?)?(?:\(\d{3}\)|\d{3})[-. ]?\d{3}[-. ]?\d{4}$/(555) 123-4567 555-123-4567 +1 555.123.4567 5551234567555-1234 (555 123-4567 +44 20 7946 0958US/Canada (NANP) format only. It checks the shape, not whether the area code exists.
/^\+[1-9]\d{1,14}$/+14155552671 +44207946095814155552671 +0123 +1 415 555 2671E.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

PatternMatchesDoes not matchCaveat
/^https?:\/\/(?:[a-z0-9-]+\.)+[a-z]{2,}(?::\d{2,5})?(?:[/?#]\S*)?$/ihttps://example.com http://sub.example.co.uk:8080/path?q=1#topftp://example.com https://localhost example.com https://exa mple.comValidates a whole http(s) URL with a domain name. Rejects localhost and IP addresses. For real validation, new URL() is stricter.
/https?:\/\/[^\s<>"']+/gsee https://example.com/page?x=1 nowno link here www.example.comFinds links in text. It keeps trailing punctuation, so "https://example.com." includes the dot.
/^[a-z0-9]+(?:-[a-z0-9]+)*$/my-blog-post post2My-Post double--dash -startURL 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.8256.1.1.1 192.168.01.1 1.2.3IPv4 with each part 0 to 255 and no leading zeros. Does not cover IPv6.
/^#(?:[0-9a-fA-F]{3}){1,2}$/#fff #1A2b3Cfff #ffff #12345gCSS 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

PatternMatchesDoes not matchCaveat
/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/2026-11-17 1999-01-312026-13-01 2026-1-5 17-11-2026ISO 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/200013/01/2026 1/5/2026 2026/11/17US 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:5924:00 9:30 12:6024-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 PM13:00 PM 0:30 AM 9:3012-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

PatternMatchesDoes not matchCaveat
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{8,}$/Passw0rd! xY7#abcdpassword 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 User123jo john-doe this_name_is_way_too_longUsername 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

PatternMatchesDoes not matchCaveat
/^[^\s@]+@[^\s@]+\.[^\s@]+$/jane.doe@example.com a+tag@mail.co.ukjane@ jane@example jane doe@example.comA practical email check, not RFC 5322. The only real test is sending a confirmation email.
/^-?\d+(?:\.\d+)?$/42 -3.14 0.53. .5 1,000 1e5Integer or decimal with a dot.
/^\d{1,3}(?:,\d{3})*(?:\.\d+)?$/1,234 1,234,567.89 9991234,567 12,34 ,123Number with comma thousands separators (US style).
/^\d{5}(?:-\d{4})?$/90210 12345-67891234 123456 12345-678US ZIP and ZIP+4. Other countries need their own pattern.

Regex for cleaning up text

PatternMatchesDoes not matchCaveat
/#[\p{L}\p{N}_]+/guloving #regex and #caféno tags # aloneFinds hashtags, including accented letters, thanks to the u flag and \p{L}.
/\.(?:jpe?g|png|gif|webp)$/iphoto.JPG img.webpphoto.jpg.exe notes.txtFile extension check on a file name. It does not prove the file type.
/\b(\w+)\s+\1\b/githis is is a test The the endthis is a testFinds 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.

  1. Describe the pattern in a sentence, including whether you want to validate a whole value or find matches in text.
  2. Read the explanation and the example results. A cross means the pattern got that example wrong.
  3. Paste your own samples into the live tester. Matches are highlighted and capture groups are listed; you can toggle the g, i, m, s, u and y flags.
  4. 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.

Related Articles

Try Our Smart Tools

Put what you've learned into practice with our free AI, SEO and developer tools.