Regex for username
A typical username regex is ^[a-zA-Z0-9_]{3,16}$ : letters, digits and underscores, 3 to 16 characters. To require a letter first and allow dots or dashes inside, use ^[a-zA-Z][a-zA-Z0-9._-]{2,15}$. Keep the rule short and show it to the user in plain words.
Syntax
/^[a-zA-Z0-9_]{3,16}$/Examples
["jane_doe", "jd", "jane doe", "Jane99"].map(s => /^[a-zA-Z0-9_]{3,16}$/.test(s))Result: [true, false, false, true]
Too short and a space both fail.
/^[a-z](?!.*\.\.)[a-z0-9._]{2,29}$/.test("jane.doe_1")Result: true
Lowercase letter first, no double dots, 3 to 30 characters total.
"@jane_doe says hi".match(/@(\w{3,16})\b/)[1]Result: "jane_doe"
Capture the handle after an @.
/^[\p{L}\p{N}_]{3,16}$/u.test("josé_99")Result: true
Unicode letters and numbers with the u flag.
Common mistakes
- Using \w{3,16} and expecting only letters and digits; \w also allows underscore, which is usually fine but should be deliberate.
- Allowing a leading dot, dash or underscore, which makes usernames like ._ or --- valid; require a letter or digit first.
- Counting length in UTF-16 units: without the u flag an emoji counts as two characters toward the {3,16} limit.
- Checking only the regex and not reserved names; add a negative lookahead such as ^(?!admin$|root$) or a separate list.
Practice Regex for username
These drills check your answer on live data, free, no account needed.
- Ranges: a letter and two digitsThe whole line must match. Match one lowercase letter followed by exactly two digits, like a12.
- URL slugsThe whole line must match. Match a URL slug: lowercase letters and digits in chunks joined by single hyphens. No hyphen at the start or end and no doubled hyphens.
- Unicode letters with the u flagThe whole line must match. Match a single word made only of letters from any language, such as café or Zoë. Digits, spaces and punctuation are not allowed.
Questions
How do I allow a hyphen in the username regex?
Put the hyphen first or last in the class, [a-zA-Z0-9_-], or escape it as \-. In the middle of a class it creates a range.
What is the regex for a username with no spaces?
^\S{3,16}$ allows any non-whitespace characters. Most sites prefer an explicit allow list such as ^[a-zA-Z0-9_]{3,16}$, which rules out punctuation as well.
How do I make the username regex case-insensitive?
Add the i flag and drop the uppercase range: /^[a-z0-9_]{3,16}$/i. Store a lowercase copy if usernames must be unique regardless of case.
Related
- Regex for alphanumericThe ^[a-zA-Z0-9]+$ pattern, how it differs from \w, how to make a URL slug by replacing everything else, and a Unicode version with \p{L} and \p{N}.
- Ranges in character classesHow character ranges work by code point, how to combine them, the invalid [a-Z] and surprising [A-z], and how to express numeric ranges with alternation.
- Unicode regexHow the u flag changes JavaScript regex, matching any letter with \p{L}, a script with \p{Script=...}, emoji as one character, and ASCII limits of \w and \b.
- Lookahead (?=...) and (?!...)How lookahead checks what follows without consuming it: numbers before a unit, words without a prefix, thousands separators and file name filters.