AWS Builder Center
When Regex Meets Unicode: A Simple Lesson About Character Validation

When Regex Meets Unicode: A Simple Lesson About Character Validation

A practical look at how regular expressions handle Unicode characters, using a real-world validation issue with German characters like ä, ö, ü, and ß. Learn why ASCII-based regex patterns can reject valid international text and how Unicode-aware validation can help.

Recently, I came across an interesting validation issue while working with a web form.
The application used the following regular expression:
^[A-Za-z0-9 &.()-]+$

At first glance, this looks like a simple way to allow letters, numbers, spaces, and a few special characters.
But there is an important limitation: it only allows the English alphabet.

What happens with German characters?

Consider the following value:
Baden-Württemberg

The character  ü  is not included in:
A-Z
a-z

As a result, the regular expression rejects the value.
The same problem can occur with other characters:
ä
ö
ü
Ä
Ö
Ü
ß

These characters are perfectly valid in many languages, but they are not part of the basic ASCII alphabet.

ASCII vs. Unicode

ASCII is a limited character set that includes the English alphabet, numbers, and common symbols.
Unicode is much broader and supports characters from languages around the world.
For example:
ASCII:
Baden-Wuerttemberg

Unicode:
Baden-Württemberg

Both represent the same place, but a strict ASCII-based regex treats them differently.

A More Flexible Approach

Modern JavaScript supports Unicode property escapes.
For example:
/^[\p{L}\p{N} &.()-]+$/u

Here:
  •  \p{L}  represents Unicode letters
  •  \p{N}  represents Unicode numbers
  •  u  enables Unicode-aware matching
This makes the expression much more suitable for international text.
Of course, the right validation rule depends on the purpose of the field. A username, an AWS resource identifier, and a person's location should not necessarily use the same validation rules.

The Main Lesson

This experience reminded me that validation rules should match the type of data they are validating.
If an application supports international users, restricting text to  A-Z  and  a-z  can create unexpected problems.
A dropdown may provide a perfectly valid value such as Baden-Württemberg, but a backend validation rule can still reject it if Unicode characters are not supported.
For me, this was a small but useful lesson about the importance of understanding regex, ASCII, and Unicode when working with web applications.
Have you encountered similar Unicode or regex validation issues?
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article