Try it yourself with our free Regex Tester tool — runs entirely in your browser, no signup needed.

How to Validate email addresses with regex in JavaScript

How to Validate Email Addresses with Regex in JavaScript

Validating email addresses is a crucial step in many web applications, ensuring that users provide a correct and functional email address. In this article, we will explore how to use regular expressions (regex) in JavaScript to validate email addresses efficiently and effectively.

Quick Example

Here is a minimal, copy-pasteable code example that solves the most common use case:

const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
const email = 'example@example.com';

if (emailRegex.test(email)) {
  console.log('Email is valid');
} else {
  console.log('Email is invalid');
}

This code uses a regex pattern to match the general format of an email address and logs whether the provided email is valid or not.

Step-by-Step Breakdown

Let's walk through the code line by line:

  • const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;:
    • This line defines a regex pattern as a constant. The pattern consists of several parts:
      • ^ matches the start of the string.
      • [a-zA-Z0-9._%+-]+ matches one or more alphanumeric characters, dots, underscores, percent signs, plus signs, or hyphens. This matches the local part of the email address (before the @).
      • @ matches the @ symbol.
      • [a-zA-Z0-9.-]+ matches one or more alphanumeric characters, dots, or hyphens. This matches the domain name.
      • \. matches a dot ( escaped with a backslash because . has a special meaning in regex).
      • [a-zA-Z]{2,} matches the domain extension (it must be at least 2 characters long).
      • $ matches the end of the string.
  • const email = 'example@example.com';:
    • This line defines a variable email with a sample email address.
  • if (emailRegex.test(email)) { ... }:
    • This line uses the test() method of the regex object to test whether the email address matches the pattern. If it does, the code inside the if statement is executed.

Handling Edge Cases

Here are some common edge cases to consider:

Empty/Null Input

To handle empty or null input, you can add a simple check before testing the email address:

if (email === null || email === '') {
  console.log('Email is empty or null');
} else if (emailRegex.test(email)) {
  console.log('Email is valid');
} else {
  console.log('Email is invalid');
}

Invalid Input

To handle invalid input, you can use a try-catch block to catch any errors that occur during the regex test:

try {
  if (emailRegex.test(email)) {
    console.log('Email is valid');
  } else {
    console.log('Email is invalid');
  }
} catch (error) {
  console.log('Error validating email:', error);
}

Large Input

To handle large input, you can use a more efficient regex pattern that uses a possessive quantifier (++) to reduce backtracking:

const emailRegex = /^[a-zA-Z0-9._%+-]++@[a-zA-Z0-9.-]++\.[a-zA-Z]{2,}$/;

Unicode/Special Characters

To handle Unicode characters and special characters, you can use a regex pattern that includes Unicode character classes and escapes special characters:

const emailRegex = /^[\p{L}\p{N}_%+-]+@[a-zA-Z0-9.-]+\.[\p{L}]{2,}$/u;

Note the u flag at the end of the regex pattern, which enables Unicode support.

Common Mistakes

Here are some common mistakes developers make when using regex to validate email addresses:

1. Not escaping special characters

Wrong code:

const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

Corrected code:

const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$/;

2. Not handling edge cases

Wrong code:

if (emailRegex.test(email)) {
  console.log('Email is valid');
} else {
  console.log('Email is invalid');
}

Corrected code:

if (email === null || email === '') {
  console.log('Email is empty or null');
} else if (emailRegex.test(email)) {
  console.log('Email is valid');
} else {
  console.log('Email is invalid');
}

3. Using a too-permissive pattern

Wrong code:

const emailRegex = /.+@.+\..+/;

Corrected code:

const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

Performance Tips

Here are some practical performance tips for validating email addresses with regex in JavaScript:

1. Use a pre-compiled regex pattern

Instead of defining a new regex pattern every time you need to validate an email address, pre-compile the pattern and store it in a variable:

const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

2. Use a possessive quantifier

Using a possessive quantifier (++) can reduce backtracking and improve performance:

const emailRegex = /^[a-zA-Z0-9._%+-]++@[a-zA-Z0-9.-]++\.[a-zA-Z]{2,}$/;

3. Avoid using unnecessary character classes

Avoid using character classes that are not necessary for the pattern, as they can slow down performance:

const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

FAQ

Q: What is the best regex pattern for validating email addresses?

A: The best regex pattern for validating email addresses is a matter of debate, but a good starting point is the pattern used in this article: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$.

Q: How do I handle internationalized domain names (IDNs)?

A: To handle IDNs, you can use a regex pattern that includes Unicode character classes and escapes special characters: ^[\p{L}\p{N}_%+-]+@[a-zA-Z0-9.-]+\\.[\p{L}]{2,}$/u.

Q: Can I use a library to validate email addresses instead of regex?

A: Yes, there are several libraries available that can validate email addresses, such as validator.js. However, using a regex pattern can be a lightweight and efficient solution.

Q: How do I handle email addresses with non-ASCII characters?

A: To handle email addresses with non-ASCII characters, you can use a regex pattern that includes Unicode character classes and escapes special characters: ^[\p{L}\p{N}_%+-]+@[a-zA-Z0-9.-]+\\.[\p{L}]{2,}$/u.

Q: Can I use this regex pattern to validate email addresses in other programming languages?

A: While the regex pattern used in this article is specific to JavaScript, similar patterns can be used in other programming languages. However, the syntax and features of regex may vary between languages.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp