regex pattern · ready to copy
Regex for matching usernames (3-20 chars, alphanum + underscore)
Common social/forum username constraint. Letters, digits, underscores. Starts with letter.
intermediate
javascript / pcre / python4 use cases
The pattern
^[a-zA-Z][a-zA-Z0-9_]{2,19}$
Test cases
| Input | Result |
|---|---|
| alice_99 | ✓matches |
| Bob123 | ✓matches |
| x_factor | ✓matches |
| 1starts_with_digit | ✗rejects |
| ab (too short) | ✗rejects |
| _underscore_first | ✗rejects |
| has-dash | ✗rejects |
Edge cases & caveats
Different platforms have different rules (Twitter: 4-15 alphanum + `_`; GitHub: dash allowed, 39-char max). Adapt the length and char-class to your spec rather than copying blindly.
Common use cases
- signup form validation
- username availability checks
- import data scrubbing
- anti-bot heuristic
Try variations against your data
regexlab is a free in-browser tester with side-by-side match highlighting, group inspector, and named-capture export to JS/Python/PCRE.
Open regexlab
Related
jwt · url · iso 8601 date · semver version