Regex for Alphabetic Characters

Alphabetic strings are a common occurrence used in forms all across the Internet. In this article, we’ll look at how one can check if a string is entirely alphabetic. Here’s the gist of it:

A regular expression for alphabetic characters should check that the expression contains one or more letters of the alphabet in either upper (A to Z) or lower case (a to z), and does not contain any other characters such as numbers or special characters.

/^[A-Za-z]+$/Edit with Regexity

Let’s find out how this expression is put together.

ALSO READ: Regex for Alphanumeric Strings

Regular Expression for Alphabetic Characters

An alphabetic string contains only letters of the alphabet. We can thus start our expression with the following:

/[A-Za-z]/Edit with Regexity

The A-Z part will match all uppercase letters while a-z will match all lowercase letters. We place them both in square brackets to indicate that we’ll accept any of these characters in any order.

We want at least one of these characters, so we need to use the one-or-more quantifier (+) behind the square brackets to specify this:

/[A-Za-z]+/Edit with Regexity

And finally, we need to ensure that there is nothing before or after the matched string. Thus we need to include a start-of-string character (^) and end-of-string character ($) at the start and end of the expression, respectively:

/^[A-Za-z]+$/Edit with Regexity

This should match any string containing only alphabetic characters.

As an alternative to using both the A-Z and a-z character classes, we can simply use the A-z class (notice uppercase A and lowercase z) to cover both scenarios. This will work since the uppercase “A” comes before the lowercase “z” in the character sequence. Note that a-Z will not work since “Z” comes before lowercase “a” in the character sequence.

/^[A-z]+$/Edit with Regexity

ALSO READ: Regex for Matching Vowels [aeiou]

Matching Alphabetic Characters and Spaces

Sometimes it’s useful to allow a space character along with alphabetic characters (for example the space between a person’s name and surname). To match spaces along with Alphabetic characters, simply include a space character inside the square brackets:

/^[A-Za-z ]+$/Edit with Regexity

The whitespace character (\s) can also be used to match a space, although it will also match other whitespace characters like tabs, line feeds, and carriage returns.

/^[A-Za-z\s]+$/Edit with Regexity

Which Flags to Use

The alphabetic regular expressions shown above will match both upper and lowercase characters or a mix of the two. However, we could also use the case-insensitive flag (i) to indicate that characters in any case can be matched. This will allow us to delete one of the a-z pairs (doesn’t matter which one):

/^[a-z]+$/iEdit with Regexity

If you’d like to find all the alphabetic strings throughout a body of text, instead of validating if the entire string is alphabetic, you can use the global flag (g). In this case, you should also remove the start-of-string (^) and end-of-string ($) characters to show that the matched strings can be anywhere in the text body.

/[A-Za-z]+/gEdit with Regexity

Sources

The regular expressions on this page were adapted from solutions posted on Stack Overflow by anubhava on this question, and by paxdiablo and Fletcher Ripp 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...

2 thoughts on “Regex for Alphabetic Characters”

    • indeed,
      I was looking for camelCase and capitalize functions, but not a single example works (all being essentially the same) if you have to deal with diacritics or umlauts.
      /[\p{L}]/u
      – \p{L} selects Unicode “Letters” (can’t test with scripts like Kanji)
      – /u flag required for \p to work

      Reply

Leave a Comment