Match Accented Letters with Regular Expressions
Regular expressions are used for a variety of tasks but the one I see most often is input validation. Names, dates, numbers...we tend to use regular expressions for everything, even when we probably shouldn't.
The most common syntax for checking alphabetic characters is A-z
but what if the string contains accented characters? Characters like ğ
and Ö
will make the regex fail. That's where we need to use Unicode property escapes to check for a broader letter format!
Let's look at how we can use \p{Letter}
and the Unicode flag (u
) to match both standard and accented characters:
// Single word
"Özil".match(/[\p{Letter}]+/gu)
// Word with spaces
"Oğuzhan Özyakup".match(/[\p{Letter}\s]+/gu);
Using regular expressions to validate strings, especially names, is much more difficult than A-z+
. Names and other strings can be very diverse -- let's not insult users by making them provide non-accented letters just to pass validation!
This is the hardest thing I've ever had to write, much less admit to myself. I've written resignation letters from jobs I've loved, I've ended relationships, I've failed at a host of tasks, and let myself down in my life. All of those feelings were very...
Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS. What we end up doing is repeating the same properties multiple times with each browser prefix. Yuck. Another thing we...
Vertically centering sibling child contents is a task we've long needed on the web but has always seemed way more difficult than it should be. We initially used tables to accomplish the task, then moved on to CSS and JavaScript tricks because table layout was horribly...
Chris Coyier's CSS-Tricks blog is everything mine isn't. Chris' blog is rock star popular, mine is not. Chris prefers jQuery, I prefer MooTools. Chris does posts with practical solutions, I do posts about stupid video-game like effects. If I...