Regex for Alphanumeric Strings

Checking for alphanumeric input is a common practice all around the web. How can we check this input using regex?

A regular expression for an alphanumeric string checks that the string contains lowercase letters a-zEdit with Regexity, uppercase letters A-ZEdit with Regexity, and numbers 0-9Edit with Regexity. Optional quantifiers are used to specify a string length.

And the code that does all this looks like this:

/^[a-zA-Z0-9]+$/Edit with Regexity

Let’s break this down to see how it works…

ALSO READ: Regex for Alphabetic Characters

General Format of an Alphanumeric String

By definition, alphanumeric strings may only contain the following characters:

Depending on your use case, you might want to restrict the number of characters being entered as well.

Regex Code for Alphanumeric Strings

We can start building our regex by including the characters classes a-zEdit with Regexity, A-ZEdit with Regexity, and 0-9Edit with Regexity in square brackets. The square brackets indicate that we can match any of the characters in that range.

/[a-zA-Z0-9]/Edit with Regexity

This will already match any alphanumeric string, but we can make it more intelligent by specifying a specific length as well. The one-or-more quantifier +Edit with Regexity placed behind the square brackets will ensure that one or more of the characters in the square brackets are matched.

/[a-zA-Z0-9]+/Edit with Regexity

The zero-or-more quantifier *Edit with Regexity works exactly like the + quantifier above but will also match an empty string with zero characters.

/[a-zA-Z0-9]*/Edit with Regexity

If you’d like to specify a specific number of characters, you can do that by using curly brackets. For instance, the following code will match 7 alphanumeric characters:

/[a-zA-Z0-9]{7}/Edit with Regexity

The following will match 7 or more characters:

/[a-zA-Z0-9]{7,}/Edit with Regexity

And finally, this code will match between 7 and 9 characters:

/[a-zA-Z0-9]{7,9}/Edit with Regexity

If we are checking user input and want to ensure there is no white space of punctuation preceding the alphanumeric string, we can use the start-of-string and end-of-string characters at the beginning and end of the expression:

/^[a-zA-Z0-9]+$/Edit with Regexity

This expression will match any alphanumeric string of any length, but will not match empty strings.

Flags to Use With This Expression

If you’d simply like to validate user input, you do not have to use any flags. If you’d like to extract all the alphanumeric strings from a piece of text you can use the global (g) flag. However, you would also have to remove the start-of-string and end-of-string characters, since only one string can be at the start or end of the piece of text.

/[a-zA-Z0-9]+/gEdit with Regexity

ALSO READ: Regex for Matching Vowels [aeiou]

Sources

The regular expressions on this page were adapted from solutions on this article and this article, and from solutions posted on Stack Overflow by Charlie on this question.

Benjamin

Founder, owner, and sole content creator on RegexLand. Enjoys programming, blogging, and teaching others how to do the same. Read more...

Leave a Comment