Pattern library / Validation
Regex to Match Alphanumeric Characters
Checking that a string holds only letters and digits is one of the most common validation questions (alphanumeric regex, regex alphanumeric, javascript check if alphanumeric). The full-match form is one character class repeated with +: [A-Za-z0-9] covers ASCII letters and digits, and the anchors make it a whole-string validator. The interesting part is what fails — spaces, the underscore almost everybody assumes is included, every symbol, and accents, which need the Unicode variant below.
How the pattern works
- Anchored with
^and$, so it validates a whole string. Drop the anchors and keep the+for find mode:[A-Za-z0-9]+returns every run of letters and digits inside a text, splitting on spaces and punctuation. - The shorthand
\wlooks like "alphanumeric" but is[A-Za-z0-9_]— it accepts underscores, so/^\w+$/passesuser_namewhere this pattern rejects it. Reach for\wonly when you genuinely mean "word characters". - ASCII-only by design:
Grüße,Ünïcode9and non-Latin digits all fail. For real-world text use^[\p{L}\p{N}]+$with theuflag —\p{L}is any letter,\p{N}any number — and it stays RE2-safe for GA4, BigQuery and Go, see RE2 vs PCRE. - Sibling rules use the same shape: letters only
^[A-Za-z]+$, digits only^\d+$, letters plus spaces^[A-Za-z ]+$for full names. Narrow the class instead of adding lookarounds.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
abc123 | Match | letters and digits, nothing else | Try → |
RegexLab42 | Match | mixed case is fine — the class covers both A–Z and a–z | Try → |
12345 | Match | digits alone are still alphanumeric; no letter is required | Try → |
user name | No match | a space is not in the character class | Try → |
user_name | No match | underscore is punctuation, not alphanumeric — the classic surprise | Try → |
p@ssw0rd | No match | the @ symbol breaks the match | Try → |
v2.1 | No match | version strings carry a dot; they need their own pattern | Try → |
Where to use it
JavaScript (validate a form field)
const ALNUM = /^[A-Za-z0-9]+$/;
ALNUM.test('abc123'); // true
ALNUM.test('user_name'); // false
JavaScript (keep only alphanumerics)
const clean = s => s.replace(/[^A-Za-z0-9]/g, '');
clean('p@ssw0rd 42!'); // 'pssw0rd42'
HTML input pattern attribute
<input type="text" name="code"
pattern="[A-Za-z0-9]+"
title="Letters and numbers only">
Common mistakes
- Underscore is not alphanumeric, so
user_nameand evenuser namefail while/^\w+$/happily accepts_admin. If your rule really means letters, digits and underscore, switch to^[A-Za-z0-9_]+$— and know that leading underscores then pass too. - The HTML
patternattribute already matches the entire value. Writingpattern=".*[A-Za-z0-9]+.*"to "cover the whole input" backfires: the loose.*lets any string containing at least one letter or digit through, souser namepasses. Just writepattern="[A-Za-z0-9]+". - The
+requires at least one character, so an empty string fails. Swapping it for*makes empty input pass — a quiet bug if the field was meant to be required. Decide explicitly:+= required,*= optional.
FAQ
How do I check if a string is alphanumeric in JavaScript?
Use /^[A-Za-z0-9]+$/ and call test(): ALNUM.test('abc123') is true, ALNUM.test('user_name') is false. No lookahead or backreference is needed, so the same pattern runs in Python (re.fullmatch), GA4 and BigQuery.
Is underscore considered alphanumeric?
No — alphanumeric means letters and digits only. That is why user_name fails the strict pattern. Username-style rules that allow underscore (and often hyphen) use ^[A-Za-z0-9_]+$ or ^\w+$ instead.
Why does my alphanumeric regex fail on é, Ü or Arabic-Indic digits?
Because [A-Za-z0-9] is ASCII-only. Use ^[\p{L}\p{N}]+$ with the u flag (new RegExp('^[\\p{L}\\p{N}]+$', 'u')) — it accepts any Unicode letter or number, stays RE2-safe and still rejects spaces and symbols.
Related patterns
Username Regex — Validate 3–20 Char Usernames · Snake Case Regex - Validate snake_case Names · Slug Regex — Match lowercase-hyphen URL Slugs
For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.