TExcelTAD
Home › Regular Expressions › Common Patterns

Common Patterns

Here is a toolbox of everyday patterns. Each comes with an honest note about its limits — real-world formats are messier than any single regex, and the right answer is often "use regex for a quick shape check, then validate properly in code".

Dates (ISO YYYY-MM-DD)

regex · try it yourself
/\b(?<y>\d{4})-(?<m>0[1-9]|1[0-2])-(?<d>0[1-9]|[12]\d|3[01])\b/g
2025-07-31  2025-13-01  2025-02-30  25-1-1
2 matches
#1 "2025-07-31" at index 0
   group y: "2025"
   group m: "07"
   group d: "31"
#2 "2025-02-30" at index 24
   group y: "2025"
   group m: "02"
   group d: "30"

Month 13 is rejected, but February 30th passes. A regex can't sensibly know about month lengths and leap years — check the real calendar in code:

js · try it yourself
function isRealDate(s) {
  const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(s);
  if (!m) return false;
  const [y, mo, d] = m.slice(1).map(Number);
  const dt = new Date(Date.UTC(y, mo - 1, d));
  return dt.getUTCFullYear() === y && dt.getUTCMonth() === mo - 1 && dt.getUTCDate() === d;
}
console.log(isRealDate("2024-02-29"), isRealDate("2025-02-29"), isRealDate("2025-02-30"));
true false false

Times (24-hour HH:MM)

regex · try it yourself
/\b(?:[01]\d|2[0-3]):[0-5]\d\b/g
08:15 23:59 24:00 7:30 12:60
2 matches
#1 "08:15" at index 0
#2 "23:59" at index 6

Phone numbers

Formats vary hugely by country. This one accepts Vietnamese mobile numbers written as 0xxxxxxxxx or +84xxxxxxxxx, with optional spaces or dots in the usual places:

regex · try it yourself
/(?:\+84|\b0)\d{3}[ .]?\d{3}[ .]?\d{3}\b/g
0912 345 678, +84912345678, 0912.345.678, 091234567, 12345
3 matches
#1 "0912 345 678" at index 0
#2 "+84912345678" at index 14
#3 "0912.345.678" at index 28

091234567 is one digit short and 12345 is far too short. Still, this is a shape check: it doesn't know which prefixes are really assigned to carriers, and other countries use completely different lengths and groupings. For real validation of international numbers use a dedicated library (such as libphonenumber), and normalise input by stripping everything except digits and a leading + before storing it.

Email (a sensible check, not a full validator)

The official email grammar is enormous; no practical regex covers it, and the only true test is sending a message. A reasonable check: something without spaces or @, an @, a domain with at least one dot:

regex · try it yourself
/^[^\s@]+@[^\s@]+\.[^\s@]+$/gm
an.nguyen@example.com
bad@
no spaces@x.com
user@localhost
x@y.io
2 matches
#1 "an.nguyen@example.com" at index 0
#2 "x@y.io" at index 58

Note: user@localhost is technically valid but rejected here — that trade-off is usually fine for a sign-up form. In browsers, <input type="email"> already does a similar check.

URLs

Again: extraction, not validation. This pulls http(s) links out of prose and avoids trailing punctuation:

regex · try it yourself
/https?:\/\/[\w.-]+(?:\/[^\s<>"]*[^\s<>".,;:!?)])?/g
Docs: https://example.com/guide?page=2. Old: http://old.example.org, and https://x.dev!
3 matches
#1 "https://example.com/guide?page=2" at index 6
#2 "http://old.example.org" at index 45
#3 "https://x.dev" at index 73

For validation in JavaScript, use the built-in parser instead: URL.canParse(s) or new URL(s) inside try.

Hex colours

regex · try it yourself
/#(?:[0-9a-f]{3}){1,2}\b/gi
#fff #A1B2C3 #12345 #abcd #0f0f0fz
2 matches
#1 "#fff" at index 0
#2 "#A1B2C3" at index 5

#12345 (5 digits) and #abcd (4 — valid in CSS as #rgba, but not in this pattern) are rejected; #0f0f0fz fails because of the word boundary. Add {3,4}/{6}/{8} alternatives if you need alpha.

Quick reference

What Pattern Caveat
Integer ^-?\d+$ no thousands separators
Decimal ^-?\d+(?:\.\d+)?$ no .5, no exponent
ISO date ^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$ allows Feb 30
24h time ^(?:[01]\d|2[0-3]):[0-5]\d$ no seconds
Hex colour ^#(?:[0-9a-fA-F]{3}){1,2}$ no alpha
Slug ^[a-z0-9]+(?:-[a-z0-9]+)*$ ASCII only
Simple email ^[^\s@]+@[^\s@]+\.[^\s@]+$ shape check only
Trim spaces ^\s+|\s+$ (with g) trim() is simpler

Test yourself

Why can't the ISO date regex reject 2025-02-30?

  1. It lacks the g flag
  2. Regex has no practical way to know month lengths and leap years
  3. Dates need the u flag
  4. It can with \d{2}

What is the most reliable way to verify an email address?

  1. A 6000-character regex
  2. ^\S+$
  3. Send a confirmation message to it
  4. Check that it contains a dot

Which hex colour does /^#(?:[0-9a-f]{3}){1,2}$/i accept?

  1. #12345
  2. #ABCDEF
  3. #abcd
  4. fff