JavaScript Email Regex Pattern

Sarah Jul 13, 2026

When it comes to validating emails on the web, regular expressions (regex) are often the first line of defense. JavaScript, with its built-in support for regex, is a powerful tool for email pattern matching. However, creating an effective and accurate email regex pattern can be challenging due to the complex structure of email addresses. Let's explore a robust JavaScript email regex pattern and understand its components.

Using JavaScript for Email Validation Regex: A Practical Guide
Using JavaScript for Email Validation Regex: A Practical Guide

Before diving into the regex pattern, it's crucial to understand that the email validation rules are defined in the Internet Engineering Task Force (IETF) standards. According to these rules, an email address has the following structure:

Using JavaScript for Email Validation Regex: A Practical Guide
Using JavaScript for Email Validation Regex: A Practical Guide

Email Components

The IETF standards break down an email address into several components:

JavaScript Regular Expressions (Regex) Cheat Sheet
JavaScript Regular Expressions (Regex) Cheat Sheet

1. Local Part: This is the part of the email address before the '@' symbol. It can contain letters, digits, and special characters, but it cannot start or end with a period.

2. Domain: This is the part of the email address after the '@' symbol. It consists of two main parts: the domain name and the Top-Level Domain (TLD).

a computer screen with some type of programming program on it's back side and the words,
a computer screen with some type of programming program on it's back side and the words,

Local Part Patterns

In JavaScript, you can use the following regex pattern to match the local part of an email address:

let localPartPattern = /^[a-zA-Z0-9._%+-]+@/;
The caret (^) asserts the start of the line, and the dollar sign ($) asserts the end of the line. The pattern includes letters (both lowercase and uppercase), digits, and the special characters ., _, %, and +. It requires at least one character (denoted by the +), and it must be followed by the '@' symbol.

Domain Patterns

Learn About the Decorator Design Pattern in JavaScript
Learn About the Decorator Design Pattern in JavaScript

Matching the domain part of an email address is more complex due to the various possible TLDs and subdomains. Here's a JavaScript regex pattern that covers many common cases:

let domainPattern = / bygg^@?(?=.{1,}(?:\.[a-zA-Z]{2,})*[a-zA-Z]{2,}$) /;
The pattern allows for optional subdomains (denoted by the question mark (?)) and requires at least two characters for the TLD (denoted by the {2,}$). It also ensures that the domain doesn't end with a period using the negative lookahead (?!\\.)

Combining Local Part and Domain Patterns

To create a comprehensive email regex pattern in JavaScript, combine the local part and domain patterns:

an image of a computer screen with some type of text on it, and two different colors
an image of a computer screen with some type of text on it, and two different colors

The resulting pattern will cover most valid email formats, while remaining performant and easy to read. Keep in mind that no regex pattern can catch every possible email format, as the IETF standards allow for a wide range of valid email addresses.

Testing the Pattern

an image of a computer program with numbers and symbols
an image of a computer program with numbers and symbols
Destructuring Patterns in JavaScript
Destructuring Patterns in JavaScript
JavaScript Email Validation Regex: Ensuring Accuracy in User Inputs
JavaScript Email Validation Regex: Ensuring Accuracy in User Inputs
Practical Email Validation Using JavaScript: Techniques for Web Developers
Practical Email Validation Using JavaScript: Techniques for Web Developers
JavaScript Fundamental
JavaScript Fundamental
the instructions for how to draw an animal with numbers and letters on it's side
the instructions for how to draw an animal with numbers and letters on it's side
an image of a computer screen with the text fixbox displayed in red and green
an image of a computer screen with the text fixbox displayed in red and green
a diagram with numbers, letters and symbols in different languages on the same page as shown below
a diagram with numbers, letters and symbols in different languages on the same page as shown below
All important array methods in JavaScript 2024
All important array methods in JavaScript 2024
a black screen with colorful text on it
a black screen with colorful text on it
an image of a computer screen with the words body and name
an image of a computer screen with the words body and name
Express.js req vs res Objects Explained — Visual Breakdown for Beginners
Express.js req vs res Objects Explained — Visual Breakdown for Beginners
the ultimate guide to creating an in - app for your website
the ultimate guide to creating an in - app for your website
Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes
JAVASCRIPT ARRAY METHODS
JAVASCRIPT ARRAY METHODS
JavaScript Reserved Words: What You Need to Know 🚀
JavaScript Reserved Words: What You Need to Know 🚀
a flyer for an upcoming event with information about the date and time in each section
a flyer for an upcoming event with information about the date and time in each section
an image of a web page with different colors and font on the bottom, including two separate
an image of a web page with different colors and font on the bottom, including two separate
Milan Jovanović on LinkedIn: What is the REPR design pattern?    It's often mentioned together with… | 35 comments
Milan Jovanović on LinkedIn: What is the REPR design pattern? It's often mentioned together with… | 35 comments
a poster with different types of projects and instructions for students to use in the classroom
a poster with different types of projects and instructions for students to use in the classroom

To test the combined email regex pattern, you can use the following JavaScript code:

let emailPattern = /^[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$/;
This code creates a variable called emailPattern and assigns it the combined email regex pattern.
let isValidEmail = emailPattern.test('example@email.com');
This code tests the emailPattern against the string 'example@email.com' and stores the result in the isValidEmail variable.

You can replace 'example@email.com' with any email address you want to test.

Implementing the Pattern

To implement the email regex pattern in a real-world application, such as a web form, you can use it to validate user input before submitting the form:

<form id="contactForm" action="#" method="POST">
This is a sample HTML form element with an id of 'contactForm'.
<input type="email" id="email" name="email" required pattern="[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}" title="Please enter a valid email address">
This is an input element with type 'email', id 'email', and name 'email'. The 'required' attribute ensures that the user must fill out this field, and the 'pattern' attribute specifies the email regex pattern. The 'title' attribute provides a message to display to users if their input is invalid.

The browser will automatically validate the email input using the specified regex pattern when the user submits the form.

In conclusion, creating an effective and accurate email regex pattern for JavaScript involves understanding the structure of email addresses and crafting a regex pattern to match that structure. While no pattern can catch every possible email format, the pattern discussed in this article covers a wide range of valid email addresses and is suitable for most use cases. By using this pattern, you can ensure that your web applications have robust email validation, improving the user experience and maintaining the integrity of your data.