Let's stop using [a-zA-Z]+
Till Sanders
Posted on March 8, 2021
If you, like me, regularly (see what I did here?) validate alphanumeric fields using Regex, you probably learned to do it like this:
'Till'.match(/[a-zA-Z0-9]+/gu)
This is technically correct, of course. And it's what most validation libraries will do when you tell them a field is alpha
/ alphanumeric
/ etc.
However, I have a problem with this approach and a lot (!) of other people do, too. Because I'm from Germany. More specifically, from a town called Lüdenscheid. And Lüdenscheid won't match the regular expression above because of the Umlaut. Same applies for languages like French, Spanish, Czech, just to name a few.
So how can we as developers be more inclusive towards languages other than English? Do we have to include all possible variations of the latin alphabet? That's a common suggestion, but of course, it doesn't scale well.
Luckily, Unicode has us covered:
'Lüdenscheid'.match(/[\p{Letter}\p{Mark}]+/gu)
The \p
flag allows us to pick a so called Unicode Character Category. In Unicode, all characters are sorted into categories that we can use in our regular expression. The Letter
category includes letters from all kinds of languages, not just A-Z. But it does not include, e.g. <
, >
, +
or $
which is important for security. The Mark
category – as lionelrowe pointed out in the comments (thanks) – contains combining marks. In Unicode, a letter like ü
can be either one or two combined code points. So depending on how the character is coded, we need the Mark
category.
More details on the Mark category
If we omit the Mark
category and run the following Regex: 'Lüdenscheid'.match(/[\p{Letter}]+/gu)
it will match Lüdenscheid
, if the ü
is encoded as a single character. On the other hand, if the ü
is encoded as a letter-mark-combination (u + ̈
), the regex will only match Lu
, because it will stop at the ̈
mark.
Browser support
Browser support for this feature is good, IE (not Edge) being the only exclusion.
Bonus
// Match only letters
'Lüdenscheid'.match(/[\p{Letter}\p{Mark}]+/gu)
// Match letters and spaces
'Pražští filharmonici'.match(/[\p{Letter}\p{Mark}\s]+/gu)
// Match letters and hyphens
'Île-de-France'.match(/[\p{Letter}\p{Mark}-]+/gu)
// Match letters hyphens and spaces
'Île-de-France'.match(/[\p{Letter}\p{Mark}\s-]+/gu)
Posted on March 8, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.