Regex for Alphanumeric Strings
^[a-zA-Z0-9]+$ accepts strings made only of ASCII letters and digits: no spaces, punctuation, underscores or accented letters. It's a common rule for codes, IDs and simple usernames.
/^[a-zA-Z0-9]+$/How it works, token by token
| Token | Meaning |
|---|---|
| ^ | start of string |
| [a-zA-Z0-9]+ | one or more ASCII letters (either case) or digits |
| $ | end of string |
What it matches
✓
abc123✓
ABC✓
2026✗
abc 123✗
abc_123✗
café✗
Try it live
One candidate per line — the m flag makes the ^ and $ anchors apply to each line. Edit anything; it runs in your browser.
/
Variations
^[a-zA-Z0-9 ]+$also allow spaces^[\p{L}\p{N}]+$letters and digits in any language — needs the u flag in JavaScriptLanguage notes
- \w is not the same thing: it also matches the underscore, and in Python 3 it matches Unicode letters by default.
Frequently asked questions
Why not use \w+?
\w includes the underscore (and, in some languages, accented letters), so it's broader than letters and digits. Spell out [a-zA-Z0-9] when that's what you mean.
How do I require at least one letter and one digit?
Add lookaheads: ^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z0-9]+$.
Related patterns
Building something custom? The regex tester gives you live match highlighting for any pattern.