Regex for Matching Alphanumeric Characters Only

Matches a string containing only letters and digits, with no punctuation or whitespace.

Published October 11, 2026

This regex matches strings made only of ASCII letters and digits.

How the pattern works

Breaking the pattern down: [A-Za-z0-9] = letters and digits; ^ $ = whole string.

Test your regex against both matching and non-matching inputs; use the Regex tool on this site to experiment live.

Pattern tests

/^[A-Za-z0-9]+$/
✓ 'abc123'
✓ 'A1'
✗ 'abc_123'
✗ 'hello world'

Practical examples

The pattern

const re = /^[A-Za-z0-9]+$/

Test it

const re = /^[A-Za-z0-9]+$/
re.test('abc123')   // true
re.test('A1')   // true
re.test('abc_123')   // false
re.test('hello world')   // false

In Python

import re
re.search(r'^[A-Za-z0-9]+$', text)

Common mistakes

  • Allowing underscores accidentally (\w includes _).
  • Rejecting valid Unicode text.
  • Matching empty strings with *.

Key takeaways

  • Anchors (^ and $) make the whole string match instead of a part of it.
  • Regex checks format only; verify data separately when it matters.
  • Test with valid and invalid samples.

FAQ

Allow underscores?

Use ^\w+$ or add _ to the class.

Where can I test this pattern?

Use the Regex Tester on this site, or regex101.com.

More General articles