Quick answer
The alphanumeric regex is ^[A-Za-z0-9]+$: one or more ASCII letters or digits and nothing else, so abc123 and Playxoft match, while spaces, hyphens, underscores, accented letters and empty strings do not. For names in any language, use ^[\p{L}\p{N}]+$ with the u flag, which accepts letters and digits from every script, such as café and 東京.
- Tested examples
- Token-by-token breakdown
- Code in 5 languages
- Live tester
How to use the alphanumeric regex
- 1Copy the pattern with Copy pattern in the tester, or take a ready-made snippet for your language from the code section below.
- 2Check the sample lines in Test string: every line the alphanumeric regex accepts is highlighted, rejected lines stay plain.
- 3Paste your own values one per line to see which pass, and read Explanation for what each part of the pattern does.
- 4Open the Code tab for JavaScript, Python, Go, C#, PHP or Dart with the pattern translated and escaped.
- 5Validate again on the server: client-side checks give users quick feedback, not security.
How does the alphanumeric regex work?
Read it left to right. Each piece below matches one part of the input:
^Start of the string.[A-Za-z0-9]+One or more characters, each an uppercase letter, a lowercase letter or a digit.$End of the string.
Which strings does the alphanumeric regex match?
The whole input must match, from the first character to the last. Every row below is checked against the pattern by our automated test suite before each release.
| Input | Matches? | Why |
|---|---|---|
abc123 | Yes | Letters and digits. |
ABC | Yes | Letters only. |
2026 | Yes | Digits only. |
Playxoft | Yes | Mixed case. |
abc 123 | No | A space. |
abc-123 | No | A hyphen. |
abc_123 | No | An underscore. \w would allow it, which is why this pattern spells out the class. |
café | No | é is outside A–Z (see the Unicode variant). |
| (empty string) | No | Empty: + needs at least one character. |
Why not use \w for alphanumeric?
Because \w is "word characters", and that includes the underscore: ^\w+$ accepts abc_123 and ___. It also changes meaning between engines. In JavaScript and Go it is [A-Za-z0-9_]; in Python 3 and .NET it covers letters and digits from every script. Spelling out [A-Za-z0-9] says exactly what you mean and behaves the same everywhere.
Common neighbors:
| Need | Pattern |
|---|---|
| Letters only | ^[A-Za-z]+$ |
| Lowercase alphanumeric | ^[a-z0-9]+$ |
| Alphanumeric with spaces | ^[A-Za-z0-9]+(?: [A-Za-z0-9]+)*$ |
| Fixed length (8) | ^[A-Za-z0-9]{8}$ |
| Any script | ^[\p{L}\p{N}]+$ with the u flag |
When should I allow Unicode letters?
Whenever the field holds human text: names, cities, company names. An ASCII-only rule rejects José, Zoë, Müller and every user who writes in Chinese, Arabic, Hindi or Cyrillic, which is a poor experience and a quiet source of lost signups.
[\p{L}\p{N}] with the u flag accepts letters and numbers from every script. Two details make it robust:
- Combining marks: some keyboards produce
e+◌́instead ofé. Normalize withstr.normalize("NFC")first, or add\p{M}to the class. - Look-alikes:
а(Cyrillic) anda(Latin) look identical. For identifiers such as usernames, keep ASCII or apply a confusables check; for display names, Unicode is the right default.
Keep ASCII for codes and identifiers that machines process: coupon codes, SKUs, API keys.
How do I use the alphanumeric regex in JavaScript, Python, Go, C# and PHP?
The pattern is translated for each engine rather than pasted as is: named groups, anchors, escapes, shorthand classes and flags are adjusted so each snippet behaves as closely to the JavaScript version as its engine allows. Differences that remain (Go's RE2 has no lookarounds or backreferences, for example) are called out under each one. The Code tab in the tester above also exports Dart, and updates as you edit the pattern.
JavaScript and TypeScript
const regex = /^[A-Za-z0-9]+$/;
function isValid(value) {
return regex.test(value);
}
console.log(isValid("abc123"));Python
import re
pattern = re.compile(r"^[A-Za-z0-9]+\Z")
def is_valid(value: str) -> bool:
return pattern.search(value) is not None
print(is_valid("abc123"))- JavaScript's $ only matches at the very end of the string. Python's $ also matches before a trailing newline, so the pattern uses \Z instead.
Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[A-Za-z0-9]+$`)
fmt.Println(re.MatchString("abc123"))
}C#
using System;
using System.Text.RegularExpressions;
// The timeout stops a pathological input from hanging the thread (ReDoS).
var regex = new Regex(@"^[A-Za-z0-9]+\z", RegexOptions.None, TimeSpan.FromSeconds(1));
Console.WriteLine(regex.IsMatch("abc123"));- JavaScript's $ only matches at the very end of the string. .NET's $ also matches before a trailing newline, so the pattern uses \z instead.
PHP
<?php
$pattern = '/^[A-Za-z0-9]+$/D';
function isValid(string $value, string $pattern): bool
{
return preg_match($pattern, $value) === 1;
}
var_dump(isValid('abc123', $pattern));- The D modifier makes $ match only at the very end, like JavaScript (PCRE's $ also matches before a trailing newline).
Which related regex patterns are useful?
- Regex for Numbers Only: Digits only, plus variants for signs, decimals and thousands separators.
- Regex for Usernames: 3–16 characters, starts with a letter, single separators between letters and digits.
- Regex for URL Slugs: Lowercase letters and digits separated by single hyphens, like regex-tester.
- Regex to Trim Leading and Trailing Whitespace: Whitespace at the start or end of a string, for trimming with replace().
Browse all 25 patterns in the regex pattern library, or build your own in the regex tester.
Frequently asked questions
Last updated by the Playxoft engineering team.