★ Identity & contact · regex
Regex for Username
A common handle rule: starts with a letter, 3 to 16 characters, letters, digits and underscores. Adjust the length range to your product.
- Runs in your browser
- No signup
Try the username pattern
Read it in plain English (best-effort)
- ^start of a line
- [a-zA-Z]one character from: a–z, A–Z
- [a-zA-Z0-9_]one character from: a–z, A–Z, 0–9, '_'
- {2,15}between 2 and 15 of the previous
- $end of a line
One sample per line works well with the m flag on anchored patterns.
The pattern, piece by piece
/^[a-zA-Z][a-zA-Z0-9_]{2,15}$/| Part | What it does |
|---|---|
^[a-zA-Z] | must start with a letter |
[a-zA-Z0-9_]{2,15} | 2–15 more letters, digits or underscores |
$ | 3 to 16 characters in total |
What it accepts and rejects
Every sample below is checked automatically in JavaScript and Python. The tester above is preloaded with all of them, one per line.
| Should match | |
|---|---|
himanshu | ✓ |
dev_42 | ✓ |
Ab_c | ✓ |
jane_doe_2026 | ✓ |
abc | ✓ |
A1234567890123_x | ✓ |
| Should not match | |
|---|---|
ab | ✗ |
1user | ✗ |
user name | ✗ |
user-name | ✗ |
_user | ✗ |
averyveryverylongusername | ✗ |
Copy it into your code
RE2-safeNo lookarounds or backreferences, so the same pattern works in Go's RE2 engine and in Rust's regex crate.
const usernameRe = /^[a-zA-Z][a-zA-Z0-9_]{2,15}$/;
export function isValidUsername(value) {
return usernameRe.test(value.trim());
}When not to use regex for this
Uniqueness, reserved words (admin, support) and case-insensitive collisions (Jane vs jane) need a database check, not a pattern. Lowercase usernames before storing to avoid look-alikes.
Questions engineers actually ask
Should usernames be case-sensitive?
Why must it start with a letter?
What is catastrophic backtracking?
More identity & contact patterns
- US Phone NumberNANP numbers in common formats, capturing area code, exchange and line.^(?:\+?1[ .-]?)?\(?([2-9][0-9]{2})\)?[ .-]?([2-9][0-9]{2})[ .-]?([0-9]{4})$
- E.164 Phone NumberInternational numbers in E.164: +, country code, up to 15 digits.^\+[1-9][0-9]{6,14}$
- US ZIP CodeFive-digit ZIP codes with an optional ZIP+4 suffix.^[0-9]{5}(?:-[0-9]{4})?$
- Strong Password12+ characters with lowercase, uppercase, a digit and a symbol.^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{12,}$
- Credit Card Number Format16-digit cards in groups of four, and 15-digit Amex in 4-6-5. Format only.^(?:(?:\d{4}[ -]?){3}\d{4}|3[47]\d{2}[ -]?\d{6}[ -]?\d{5})$
Or browse the full regex library.
More from the workbench
Fine print
Tools: on the house.
Engineer: by the hour.
The regex was free. Code that doesn't need one is the upgrade: typed schemas, validated once at the edge, trusted everywhere after.
I'm Himanshu, an AI and full-stack engineer. I build MVPs, AI agents and the growth plumbing around them for early-stage startups.
