Advertisement

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.

The pattern
^[A-Za-z0-9]+$
Load this pattern into the tester →

How the pattern works

Test it: match & no-match examples

Every row is loaded straight into the tester — click Try to see the match live:

InputResultWhy
abc123Matchletters and digits, nothing elseTry →
RegexLab42Matchmixed case is fine — the class covers both A–Z and a–zTry →
12345Matchdigits alone are still alphanumeric; no letter is requiredTry →
user nameNo matcha space is not in the character classTry →
user_nameNo matchunderscore is punctuation, not alphanumeric — the classic surpriseTry →
p@ssw0rdNo matchthe @ symbol breaks the matchTry →
v2.1No matchversion strings carry a dot; they need their own patternTry →

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

Test this pattern on your own data
Paste it into the free tester with your real strings — nothing leaves your browser.
Open the regex tester →

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.

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.