Text & cleanup
Everyday text patterns that work anywhere: numbers only, letters only, no special characters, usernames, extra whitespace, extracting numbers, whole-word search and removing duplicate lines.
- Numbers only
/^\d+$/gmRegex that allows digits only, 0-9 and nothing else. Copy the JavaScript pattern, see examples, variants for decimals and negatives, and test it live. - Letters only
/^[A-Za-z]+$/gmRegex that allows letters only, A-Z and a-z, with variants for spaces and accented characters. Copy the JavaScript pattern and test it live. - No special characters
/^[A-Za-z0-9 ]+$/gmRegex that allows only letters, digits and spaces, and rejects special characters. Copy the JavaScript pattern, learn how to strip them, and test it live. - Username
/^(?=.{3,16}$)[A-Za-z][A-Za-z0-9_]*$/gmRegex to validate a username: 3 to 16 characters, starts with a letter, then letters, digits or underscores. Copy the pattern and test it live. - Extra whitespace
/^[ \t]+|[ \t]+$|[ \t]{2,}/gmRegex to find leading, trailing and repeated spaces, plus snippets to trim and collapse whitespace in JavaScript. Copy the pattern and test it live. - Extract numbers
/-?\d+(?:\.\d+)?/gRegex to extract all numbers from a string, including negatives and decimals. Copy the JavaScript pattern, see examples and test it live. - Whole word match
/\bcat\b/giRegex to match a whole word only, so cat does not match category. Learn the \b word boundary, case-insensitive search and escaping. Test it live. - Remove duplicate lines
/^(.*)(?:\r?\n\1)+$/gmRegex that finds consecutive duplicate lines so you can replace them with one copy, plus a JavaScript Set method for non-adjacent duplicates. Test it live.
India
Formats used in Indian forms, KYC, GST and payments: PAN, Aadhaar, GSTIN, IFSC, PIN code, mobile numbers, vehicle plates and UPI IDs.
- PAN card number
/\b[A-Z]{5}[0-9]{4}[A-Z]\b/gRegex to validate an Indian PAN card number (5 letters, 4 digits, 1 letter). Copy the JavaScript pattern, see matching examples, and test it live in your browser. - Aadhaar number
/\b[2-9]\d{3}\s?\d{4}\s?\d{4}\b/gRegex for a 12-digit Aadhaar number with optional spaces between groups of four. Copy the pattern, view examples, and test it privately in your browser. - GSTIN
/\b\d{2}[A-Z]{5}\d{4}[A-Z][1-9A-Z]Z[0-9A-Z]\b/gRegex to validate the format of an Indian GSTIN (15 characters: state code, PAN, entity number, Z, checksum). Copy the pattern and test it live. - IFSC code
/\b[A-Z]{4}0[A-Z0-9]{6}\b/gRegex to validate an Indian IFSC code: 4 bank letters, a zero, and 6 branch characters. Copy the pattern, see examples and test it live. - PIN code
/\b[1-9]\d{2}\s?\d{3}\b/gRegex for a 6-digit Indian PIN code, optionally written with a space after the third digit. Copy the pattern, view examples and test it live. - Indian mobile number
/(?<!\d)(?:\+91[\s-]?|91[\s-]?|0)?[6-9]\d{9}(?!\d)/gRegex to match Indian mobile numbers: 10 digits starting 6-9, with optional +91, 91 or 0 prefix. Copy the JavaScript pattern and test it live. - Vehicle registration number
/\b[A-Z]{2}[ -]?\d{1,2}[ -]?[A-Z]{1,3}[ -]?\d{4}\b/gRegex for Indian vehicle registration numbers like RJ14 AB 1234 or MH-12-AB-1234. Accepts spaces or dashes. Copy the pattern and test it live. - UPI ID
/\b[a-zA-Z0-9.\-_]{2,256}@[a-zA-Z]{2,64}\b(?!\.[a-zA-Z])/gRegex to match UPI IDs in the name@handle format, without confusing them with email addresses. Copy the pattern, view examples and test it live.
Web & network
Everyday web patterns: email addresses, URLs, IPv4 addresses, UUIDs and hex colours.
- Email address
/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/gA practical email regex for JavaScript with examples, a token breakdown, and its limits. Copy the pattern and test it live in your browser. - Web URL
/https?:\/\/[^\s"'<>]+/gRegex to find http and https URLs in text, with examples, a token breakdown and caveats. Copy the JavaScript pattern and test it live. - IPv4 address
/\b(?:(?:25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|1?\d?\d)\b/gRegex that matches IPv4 addresses and rejects octets above 255. Copy the JavaScript pattern, see a token breakdown and test it live. - UUID
/\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\b/gRegex for UUIDs in the standard 8-4-4-4-12 hexadecimal format, with a stricter version-4 variant. Copy the pattern and test it live. - Hex colour
/#(?:[0-9a-fA-F]{6}|[0-9a-fA-F]{3})\b/gRegex for CSS hex colours like #fff and #1D9E75, with examples and a variant for 8-digit values. Copy the pattern and test it live.
Dates, numbers & forms
Common form-validation patterns: ISO dates, US phone numbers, ZIP codes and password rules.
- Date (YYYY-MM-DD)
/\b\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])\b/gRegex for ISO dates in YYYY-MM-DD format with real month and day ranges. Copy the JavaScript pattern, see its limits and test it live. - US phone number
/\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}/gRegex for US phone numbers in formats like (415) 555-0132, 415-555-0132 and 4155550132. Copy the pattern, see examples and test it live. - US ZIP code
/\b\d{5}(?:-\d{4})?\b/gRegex for US ZIP codes: five digits with an optional four-digit ZIP+4 extension. Copy the pattern, see examples and test it live. - Strong password
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\w\s]).{8,}$/mRegex that requires an uppercase letter, lowercase letter, digit, symbol and 8+ characters, using lookaheads. Copy the pattern and test it live.
Want to build your own? Open the regex tester.