RX

Common Patterns

Regular Expressions · 11 entries

Email Address (Simple)

syntax
/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/
example
JS:  'contact [email protected] now'.match(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/)
Py:  re.findall(r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}', text)
output
"[email protected]"

Note This is a practical pattern covering the vast majority of real email addresses. The full RFC 5322 spec is far more complex and almost never needed. For production validation, prefer a library or send a confirmation email.

URL Pattern

syntax
/https?:\/\/[\w.-]+(?:\.[a-zA-Z]{2,})(?:[\/\w._~:?#\[\]@!$&'()*+,;=-]*)/
example
JS:  'Visit https://example.com/path?q=1&lang=en#top today'.match(/https?:\/\/[\w.-]+(?:\.[a-zA-Z]{2,})(?:[\/\w._~:?#@!$&'()*+,;=-]*)/)
Py:  re.findall(r'https?://[\w.-]+(?:\.[a-zA-Z]{2,})(?:[/\w._~:?#@!$&\'()*+,;=-]*)', text)
output
"https://example.com/path?q=1&lang=en#top"

Note URL matching is inherently tricky due to the wide range of valid characters. This pattern handles most common HTTP/HTTPS URLs. For rigorous URL validation, use the URL constructor in JS or urllib.parse in Python.

IPv4 Address

syntax
/\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b/
example
JS:  '192.168.1.1 and 999.999.999.999'.match(/\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b/g)
Py:  re.findall(r'\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b', text)
output
["192.168.1.1"]  (999.999.999.999 rejected -- octets must be 0-255)

Note Each octet allows 0-255. The alternation order matters: check 25[0-5] before 2[0-4]\d before the general case. A simpler but less accurate version is \d{1,3}(\.\d{1,3}){3} which does not validate octet ranges.

Phone Number (US Formats)

syntax
/(?:\+?1[-. ]?)?\(?\d{3}\)?[-. ]?\d{3}[-. ]?\d{4}/
example
JS:  const phone = /(?:\+?1[-. ]?)?\(?\d{3}\)?[-. ]?\d{3}[-. ]?\d{4}/g
     'Call (555) 123-4567 or +1.800.555.0199'.match(phone)
output
["(555) 123-4567", "+1.800.555.0199"]

Note Phone formats vary wildly by country. This covers common US formats: (555) 123-4567, 555-123-4567, 555.123.4567, +1 555 123 4567. For international numbers, consider a library like libphonenumber.

Date Formats (YYYY-MM-DD / MM/DD/YYYY)

syntax
YYYY-MM-DD: /\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])/
MM/DD/YYYY: /(?:0[1-9]|1[0-2])\/(?:0[1-9]|[12]\d|3[01])\/\d{4}/
example
JS:  '2026-04-04 and 12/25/2025'.match(/\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])/g)
Py:  re.findall(r'(?:0[1-9]|1[0-2])/(?:0[1-9]|[12]\d|3[01])/\d{4}', text)
output
JS: ["2026-04-04"]
Py: ["12/25/2025"]

Note Regex validates format but not logical correctness -- it will accept 02/31/2026 (Feb 31 does not exist). For actual date validation, parse the match with Date (JS) or datetime (Python) afterward.

Username Validation

syntax
/^[a-zA-Z][a-zA-Z0-9._-]{2,19}$/
example
JS:  /^[a-zA-Z][a-zA-Z0-9._-]{2,19}$/.test('dev_user.42')   // true
JS:  /^[a-zA-Z][a-zA-Z0-9._-]{2,19}$/.test('_nope')         // false
JS:  /^[a-zA-Z][a-zA-Z0-9._-]{2,19}$/.test('ab')            // false
output
true, false (starts with _), false (too short: 3-20 chars required)

Note Rules: must start with a letter, 3-20 characters total, allows letters, digits, dots, underscores, and hyphens. Adjust the {2,19} range for your app's requirements (first char + 2-19 more = 3-20 total).

Hex Color Code

syntax
/^#(?:[0-9a-fA-F]{3}){1,2}$/
example
JS:  /^#(?:[0-9a-fA-F]{3}){1,2}$/.test('#ff5733')   // true
JS:  /^#(?:[0-9a-fA-F]{3}){1,2}$/.test('#F3A')      // true
JS:  /^#(?:[0-9a-fA-F]{3}){1,2}$/.test('#xyz')      // false
output
true (6-digit), true (3-digit shorthand), false (invalid hex)

Note Matches both 3-digit (#RGB) and 6-digit (#RRGGBB) hex colors. The {1,2} quantifier on the 3-char group handles both lengths. Does not cover 8-digit RGBA hex -- extend to {1,2,} won't work; add a separate alternation for #RRGGBBAA if needed.

File Extension Extraction

syntax
/\.([a-zA-Z0-9]+)$/
example
JS:  'report-final.v2.pdf'.match(/\.([a-zA-Z0-9]+)$/)[1]
Py:  re.search(r'\.([a-zA-Z0-9]+)$', 'archive.tar.gz').group(1)
output
JS: "pdf"
Py: "gz"

Note Captures only the last extension. For double extensions like .tar.gz, match /\.tar\.gz$/ explicitly or extract both with /\.([a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*)$/. Alternatively, use path.extname (Node) or os.path.splitext (Python).

HTML Tag Matching

syntax
/<([a-z][a-z0-9]*)\b[^>]*>(.*?)<\/\1>/i
example
JS:  '<div class="box">content</div>'.match(/<([a-z][a-z0-9]*)\b[^>]*>(.*?)<\/\1>/i)
Py:  re.search(r'<([a-z][a-z0-9]*)\b[^>]*>(.*?)</\1>', html, re.IGNORECASE)
output
Full: "<div class=\"box\">content</div>"
Group 1: "div", Group 2: "content"

Note This works for simple, non-nested cases only. Regex CANNOT reliably parse nested HTML -- nested tags, self-closing tags, comments, and CDATA sections break pattern-based approaches. Use a proper HTML parser (DOMParser, BeautifulSoup, cheerio) for production code.

Whitespace Trimming

syntax
/^\s+|\s+$/g
example
JS:  '  hello world  '.replace(/^\s+|\s+$/g, '')
Py:  re.sub(r'^\s+|\s+$', '', '  hello world  ')
output
"hello world"

Note Equivalent to str.trim() in JS and str.strip() in Python. The regex version is useful when you also want to normalize internal whitespace: replace /\s+/g with ' ' after trimming.

Number with Commas / Decimals

syntax
/^-?\d{1,3}(,\d{3})*(\.\d+)?$/
example
JS:  /^-?\d{1,3}(,\d{3})*(\.\d+)?$/.test('1,234,567.89')  // true
JS:  /^-?\d{1,3}(,\d{3})*(\.\d+)?$/.test('12,34')          // false
output
true, false (incorrect comma placement)

Note Validates US-formatted numbers with optional commas every 3 digits and optional decimal part. European formats swap comma and period -- adjust the pattern accordingly. The leading -? handles negative numbers.