regexhelper
Test a pattern. Copy a ready-made one. No signup.

Regex to Match Alphanumeric Only

Copy the pattern, paste your strings, and see which are purely letters and digits. Below: why anchoring matters and how to allow underscores.

/ /

What this pattern matches

It matches a whole string made up of only ASCII letters and digits — no spaces, punctuation, or symbols.

Part by part

^ and $ anchor to the start and end of the line, so the entire value must be alphanumeric.

[A-Za-z0-9]+ — one or more letters or digits.

Common variations

Allow underscores: ^[A-Za-z0-9_]+$. Allow Unicode letters (PCRE/Python): ^[\w]+$ with the Unicode flag.

Use it in your code

Python
import re
text = "abc123\nUser_01\nhello world\nABC\n2024"
pattern = r'''^[A-Za-z0-9]+$'''
for m in re.finditer(pattern, text, re.M):
    print(m.group())
JavaScript
const text = "abc123\nUser_01\nhello world\nABC\n2024";
const re = new RegExp(String.raw`^[A-Za-z0-9]+$`, "gm");
console.log(text.match(re));

FAQ

Why does 'hello world' fail?

The space is not alphanumeric, and the anchors require every character to match, so the space breaks it.

How do I allow an empty string?

Change + to * : ^[A-Za-z0-9]*$ also matches the empty string.

Does it allow accented letters like é?

No, [A-Za-z] is ASCII only. Use \p{L} in PCRE or [^\W\d_] with re.UNICODE in Python for Unicode letters.