CodexBloom - Programming Q&A Platform

Regex Not Matching Email Addresses with International Characters in JavaScript

👀 Views: 497 💬 Answers: 1 📅 Created: 2025-06-13
regex javascript email-validation JavaScript

I'm relatively new to this, so bear with me. I'm trying to validate email addresses in my JavaScript application, and I need to ensure that the regex can handle international characters, as we have users with email addresses in various languages. The standard regex I was using is fairly simple: ```javascript const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; ``` However, this fails to match email addresses like `用户@例子.广告` or `münchen@example.com`. When I test these addresses, they return `false`, indicating they are not valid. I've tried modifying the regex to include Unicode support, but I'm not sure how to properly implement it. Here's the latest version I attempted: ```javascript const emailRegex = /^[\p{L}0-9._%+-]+@[\p{L}0-9.-]+\.[\p{L}]{2,}$/u; ``` This seems to work for most international characters, but I am still working with issues with certain valid emails, particularly those that include special characters like accents or symbols that are not part of the basic Latin alphabet. When I test an email like `éxample@domain.com`, it works, but `ñandú@example.com` fails unexpectedly. The behavior message I get is simply `false` from the regex test, which doesn’t give me much insight. Can anyone guide to adjust my regex to reliably validate international email addresses without missing valid inputs? Also, are there any best practices for handling emails with international characters in general? Any help would be greatly appreciated!