iTranslated by AI
Writing Email Regular Expressions Conforming to RFC 5321/5322
Introduction
As discussed later, validation strictly adhering to the RFC is not recommended.
The following regular expression is the most common email regex and is also specified in the HTML specification.
/^[a-zA-Z0-9.!#$%\u0026'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
Spoiler for RFC 5321 and RFC 5322 Regex
RFC 5321 Regex
/^(?=.{1,256}$)(?=(?:[^@]{1,64})@)(?!.*\.\.)(?<local>(?:[a-zA-Z0-9!#$%\u0026'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%\u0026'*+/=?^_`{|}~-]+)*|"(?:[^"\\]|\\[\s\S]){0,62}"))@(?<domain>[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+|\[(?:(?:[0-9]{1,3}\.){3}[0-9]{1,3}|IPv6:[0-9a-fA-F:]+)\])$/
RFC 5322 Regex
/^(?=.{1,998}$)(?:\s|\((?:[^()\\]|\\[\s\S])*(?:\((?:[^()\\]|\\[\s\S])*\)(?:[^()\\]|\\[\s\S])*)*\))*(?<local>"(?:[^"\\]|\\[\s\S]){0,62}"(?:\."(?:[^"\\]|\\[\s\S]){0,62}")*|"(?:[^"\\]|\\[\s\S]){0,62}"(?:\.[a-zA-Z0-9!#$%\u0026'*/=?^_`{|}~+-]{1,64}(?:\.[a-zA-Z0-9!#$%\u0026'*/=?^_`{|}~+-]{1,64})*)+|[a-zA-Z0-9!#$%\u0026'*/=?^_`{|}~+-]{1,64}(?:\.[a-zA-Z0-9!#$%\u0026'*/=?^_`{|}~+-]{1,64})*(?:\."(?:[^"\\]|\\[\s\S]){0,62}")+|[a-zA-Z0-9!#$%\u0026'*/=?^_`{|}~+-]{1,64}(?:(?:\s|\((?:[^()\\]|\\[\s\S])*(?:\((?:[^()\\]|\\[\s\S])*\)(?:[^()\\]|\\[\s\S])*)*\))*\.(?:\s|\((?:[^()\\]|\\[\s\S])*(?:\((?:[^()\\]|\\[\s\S])*\)(?:[^()\\]|\\[\s\S])*)*\))*[a-zA-Z0-9!#$%\u0026'*/=?^_`{|}~+-]{1,64})*)(?:\s|\((?:[^()\\]|\\[\s\S])*(?:\((?:[^()\\]|\\[\s\S])*\)(?:[^()\\]|\\[\s\S])*)*\))*@(?:\s|\((?:[^()\\]|\\[\s\S])*(?:\((?:[^()\\]|\\[\s\S])*\)(?:[^()\\]|\\[\s\S])*)*\))*(?<domain>[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:(?:\s|\((?:[^()\\]|\\[\s\S])*(?:\((?:[^()\\]|\\[\s\S])*\)(?:[^()\\]|\\[\s\S])*)*\))*\.(?:\s|\((?:[^()\\]|\\[\s\S])*(?:\((?:[^()\\]|\\[\s\S])*\)(?:[^()\\]|\\[\s\S])*)*\))*[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+|\[(?:(?:[0-9]{1,3}\.){3}[0-9]{1,3}|IPv6:[0-9a-fA-F:]+)\])(?:\s|\((?:[^()\\]|\\[\s\S])*(?:\((?:[^()\\]|\\[\s\S])*\)(?:[^()\\]|\\[\s\S])*)*\))*$/
2. Common Email Regular Expression (HTML Specification)
The HTML specification "4.10.5.1.5 Email state (type=email)" defines a regular expression for email addresses.
/^[a-zA-Z0-9.!#$%\u0026'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
Browsers and other tools perform validation based on this specification, making it likely the most widely used email regex in the world.
Why the HTML Specification Violates the RFC
However, the HTML specification includes the following note:
This requirement is a willful violation of RFC 5322. RFC 5322 defines the syntax for email addresses, but it is both too strict (before the "@" character) and too ambiguous (after the "@" character) to be practical here, as it also permits comments, whitespace, and quoted strings in ways that are unfamiliar to most users.
In other words, the HTML specification intentionally violates RFC 5322. It includes syntax that is unfamiliar or overly strict, making it impractical for real-world use. However, since we aim to create a validation strictly following the RFC, let's examine what is problematic in detail.
3. Basic Knowledge of Email RFCs
History of Email RFCs
The format of email addresses has been standardized through the following progression:
- RFC 822 (1982): Early message format standard
- RFC 2822 (2001): Modernized version of RFC 822
- RFC 5322 (2008): Current message format standard
This article covers the current RFC 5322 and RFC 5321 (SMTP protocol).
Difference Between RFC 5322 and RFC 5321: Scope of Definition
These two major RFCs related to email addresses define different aspects:
RFC 5322 (Message Format)
- Defines the "content" of emails, specifically the format of headers
- Defines the "syntax" of email addresses written in headers such as
To:,From: - Allows more flexible and expressive syntax (quoted-string, comments, etc.)
RFC 5321 (SMTP Protocol)
- Defines the "transfer" protocol for emails (SMTP)
- Defines the syntax of addresses used in
MAIL FROM:,RCPT TO:commands - Only permits DNS-resolvable domain names
Key Difference
Being syntactically correct per RFC 5322 is different from being actually deliverable per RFC 5321.
For example, user@example!com is syntactically valid in RFC 5322, but it cannot be delivered under RFC 5321 because it requires DNS resolution. Therefore, RFC 5321 has stricter constraints.
4. Building an RFC 5321-Compliant Regular Expression
RFC 5321 defines the format of email addresses that can actually be transferred via SMTP. We will gradually build towards RFC 5321 compliance, starting from the HTML specification's regex.
Understanding the Basic Structure
An RFC 5321 email address has the following basic structure:
local-part "@" domain
-
local-part: Maximum 64 characters -
domain: Maximum 255 characters - Entire path: Maximum 256 characters
Implementing the Local-Part
Section 4.1.2 of RFC 5321 defines local-part as dot-atom / quoted-string.
dot-atom Form
dot-atom can use atext from RFC 5322. atext includes the following characters:
- Alphanumeric:
A-Z,a-z,0-9 - Symbols: ! # $ % & ' * + - / = ? ^ _ ` { | } ~
Dots (.) can be used as separators, but consecutive dots or leading/trailing dots are not allowed.
Note that the alias feature using + (e.g., user+alias@example.com) is not defined in the RFC; it is a custom extension by Gmail and others.
Regex for dot-atom:
[a-zA-Z0-9!#$%\u0026'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%\u0026'*+/=?^_`{|}~-]+)*
Quoted-String Form
RFC 5321 also supports quoted-string as defined in RFC 5322. This allows characters not normally permitted:
"user"@example.com
"user.name"@example.com
"user@domain"@example.com
Regex for quoted-string:
"(?:[^"\\]|\\[\s\S]){0,62}"
The local-part in RFC 5321 is thus a combination of these forms:
(?:[a-zA-Z0-9!#$%\u0026'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%\u0026'*+/=?^_`{|}~-]+)*|"(?:[^"\\]|\\[\s\S]){0,62}")
Implementing the Domain (DNS Constraints)
The domain in RFC 5321 must be a DNS-resolvable name. According to RFC 1035, the following constraints apply:
- Allowed characters: alphanumeric (
A-Z,a-z,0-9) and hyphens (-) only - Hyphens cannot be at the beginning or end
- Each label is a maximum of 63 characters
- Entire domain is a maximum of 255 characters
- At least one dot (
.) is required (TLD is required)
This can be expressed with the following regex:
[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+
Implementing Domain Literals (IP Addresses)
RFC 5321 also permits domain-literal which directly specifies an IP address instead of a DNS name:
user@[192.168.0.1]
user@[IPv6:2001:db8::1]
Expressed as a regex:
\[(?:(?:[0-9]{1,3}\.){3}[0-9]{1,3}|IPv6:[0-9a-fA-F:]+)\]
Prohibition of Consecutive Dots
Consecutive dots (..) are not allowed, so we exclude them using a lookahead assertion:
(?!.*\.\.)
Length Restrictions
RFC 5321 defines a maximum path length of 256 characters and a maximum local-part length of 64 characters. Since the path includes angle brackets (user@example.com), the email address itself is practically limited to about 254 characters.
Length restrictions are implemented using lookahead assertions at the beginning of the regex:
(?=.{1,256}$)(?=(?:[^@]{1,64})@)
Final RFC 5321 Regular Expression
Combining these yields the RFC 5321-compliant regex:
/^(?=.{1,256}$)(?=(?:[^@]{1,64})@)(?!.*\.\.)(?<local>(?:[a-zA-Z0-9!#$%\u0026'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%\u0026'*+/=?^_`{|}~-]+)*|"(?:[^"\\]|\\[\s\S]){0,62}"))@(?<domain>[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+|\[(?:(?:[0-9]{1,3}\.){3}[0-9]{1,3}|IPv6:[0-9a-fA-F:]+)\])$/
5. Building an RFC 5322-Compliant Regular Expression
RFC 5322 defines the format of email addresses written in email headers and allows more flexible syntax than RFC 5321.
Implementing Quoted-String
The biggest feature of RFC 5322 is the ability to use quoted-string in the local-part. This allows characters that are normally not permitted (e.g., spaces, dots, at signs).
Examples of Quoted-String
"user name"@example.com
"user..dots"@example.com
"user@domain"@example.com
Inside quoted-string, backslashes (\) can be used for escaping:
"user\"quote"@example.com
"user\\backslash"@example.com
Regex for quoted-string:
"(?:[^"\\]|\\[\s\S]){0,62}"
Mixed Form (Combination of Quoted-String and Atext)
RFC 5322 also permits a mixed form where quoted-string and atext are concatenated with .:
"first".last@example.com
first."last"@example.com
This flexibility makes the definition of local-part complex. The following four patterns must be supported:
-
"quoted"."quoted"...- quoted-string only -
"quoted".atext.atext...- starts with quoted-string, ends with atext -
atext.atext..."quoted"- starts with atext, ends with quoted-string -
atext.atext...- atext only
Expressed as a regex:
(?<local>"(?:[^"\\]|\\[\s\S]){0,62}"(?:\."(?:[^"\\]|\\[\s\S]){0,62}")*|"(?:[^"\\]|\\[\s\S]){0,62}"(?:\.[a-zA-Z0-9!#$%\u0026'*/=?^_`{|}~+-]{1,64}(?:\.[a-zA-Z0-9!#$%\u0026'*/=?^_`{|}~+-]{1,64})*)+|[a-zA-Z0-9!#$%\u0026'*/=?^_`{|}~+-]{1,64}(?:\.[a-zA-Z0-9!#$%\u0026'*/=?^_`{|}~+-]{1,64})*(?:\."(?:[^"\\]|\\[\s\S]){0,62}")+|[a-zA-Z0-9!#$%\u0026'*/=?^_`{|}~+-]{1,64}(?:(?:\s|\((?:[^()\\]|\\[\s\S])*(?:\((?:[^()\\]|\\[\s\S])*\)(?:[^()\\]|\\[\s\S])*)*\))*\.(?:\s|\((?:[^()\\]|\\[\s\S])*(?:\((?:[^()\\]|\\[\s\S])*\)(?:[^()\\]|\\[\s\S])*)*\))*[a-zA-Z0-9!#$%\u0026'*/=?^_`{|}~+-]{1,64})*)
Comment Support
RFC 5322 allows inserting comments and whitespace at various positions in an email address:
user(comment)@example.com
(comment)user@example.com
user@(comment)example.com
user@example.(comment)com
user @ example.com
Comments are enclosed in parentheses (()) and can be nested. Inside a comment, backslashes (\) can be used for escaping.
Regex for comments:
(?:\s|\((?:[^()\\]|\\[\s\S])*(?:\((?:[^()\\]|\\[\s\S])*\)(?:[^()\\]|\\[\s\S])*)*\))*
This is placed before and after @, around the local-part and domain, and before and after dots (.) in the domain.
Example with comments added to the domain:
(?<domain>[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:(?:\s|\((?:[^()\\]|\\[\s\S])*(?:\((?:[^()\\]|\\[\s\S])*\)(?:[^()\\]|\\[\s\S])*)*\))*\.(?:\s|\((?:[^()\\]|\\[\s\S])*(?:\((?:[^()\\]|\\[\s\S])*\)(?:[^()\\]|\\[\s\S])*)*\))*[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+|\[(?:(?:[0-9]{1,3}\.){3}[0-9]{1,3}|IPv6:[0-9a-fA-F:]+)\])
Adjusting Length Limits
RFC 5322 does not have a strict path length limit like RFC 5321, but an implementation limit of 998 characters is recommended.
(?=.{1,998}$)
Final RFC 5322 Regular Expression
Combining these yields the RFC 5322-compliant regex:
/^(?=.{1,998}$)(?:\s|\((?:[^()\\]|\\[\s\S])*(?:\((?:[^()\\]|\\[\s\S])*\)(?:[^()\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[\s\S])*(?:\((?:[^()\\]|\\[\s\S])*\)(?:[^()\\]|\\[\s\S])*)*\))*$/
Test Results
Tests based on the RFC specification were executed against the implemented regex, and all test cases passed:
$ bun test src/test/email.test.ts
bun test v1.3.1
42 pass
0 fail
193 expect() calls
Ran 42 tests across 1 file. [19.00ms]
test code
import { describe, expect, it } from "bun:test";
export type ValidationLevel = "rfc5321" | "rfc5322";
interface ValidationOptions {
level: ValidationLevel;
}
const EMAIL_PATTERNS: Record<ValidationLevel, RegExp> = {
rfc5321:
/^(?=.{1,256}$)(?=(?:[^@]{1,64})@)(?!.*\.\.)(?<local>(?:[a-zA-Z0-9!#$%\u0026'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%\u0026'*+/=?^_`{|}~-]+)*|"(?:[^"\\]|\\[\s\S]){0,62}"))@(?<domain>[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+|\[(?:(?:[0-9]{1,3}\.){3}[0-9]{1,3}|IPv6:[0-9a-fA-F:]+)\])$/,
rfc5322:
/^(?=.{1,998}$)(?:\s|\((?:[^()\\]|\\[\s\S])*(?:\((?:[^()\\]|\\[\s\S])*\)(?:[^()\\]|\\[\s\S])*)*\))*(?<local>"(?:[^"\\]|\\[\s\S]){0,62}"(?:\."(?:[^"\\]|\\[\s\S]){0,62}")*|"(?:[^"\\]|\\[\s\S]){0,62}"(?:\.[a-zA-Z0-9!#$%\u0026'*/=?^_`{|}~+-]{1,64}(?:\.[a-zA-Z0-9!#$%\u0026'*/=?^_`{|}~+-]{1,64})*)+|[a-zA-Z0-9!#$%\u0026'*/=?^_`{|}~+-]{1,64}(?:\.[a-zA-Z0-9!#$%\u0026'*/=?^_`{|}~+-]{1,64})*(?:\."(?:[^"\\]|\\[\s\S]){0,62}")+|[a-zA-Z0-9!#$%\u0026'*/=?^_`{|}~+-]{1,64}(?:(?:\s|\((?:[^()\\]|\\[\s\S])*(?:\((?:[^()\\]|\\[\s\S])*\)(?:[^()\\]|\\[\s\S])*)*\))*\.(?:\s|\((?:[^()\\]|\\[\s\S])*(?:\((?:[^()\\]|\\[\s\S])*\)(?:[^()\\]|\\[\s\S])*)*\))*[a-zA-Z0-9!#$%\u0026'*/=?^_`{|}~+-]{1,64})*)(?:\s|\((?:[^()\\]|\\[\s\S])*(?:\((?:[^()\\]|\\[\s\S])*\)(?:[^()\\]|\\[\s\S])*)*\))*@(?:\s|\((?:[^()\\]|\\[\s\S])*(?:\((?:[^()\\]|\\[\s\S])*\)(?:[^()\\]|\\[\s\S])*)*\))*(?<domain>[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:(?:\s|\((?:[^()\\]|\\[\s\S])*(?:\((?:[^()\\]|\\[\s\S])*\)(?:[^()\\]|\\[\s\S])*)*\))*\.(?:\s|\((?:[^()\\]|\\[\s\S])*(?:\((?:[^()\\]|\\[\s\S])*\)(?:[^()\\]|\\[\s\S])*)*\))*[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+|\[(?:(?:[0-9]{1,3}\.){3}[0-9]{1,3}|IPv6:[0-9a-fA-F:]+)\])(?:\s|\((?:[^()\\]|\\[\s\S])*(?:\((?:[^()\\]|\\[\s\S])*\)(?:[^()\\]|\\[\s\S])*)*\))*$/,
} as const;
export const validateEmail = (email: string, options: ValidationOptions) => {
const { level } = options;
const pattern = EMAIL_PATTERNS[level];
const match = pattern.exec(email);
return {
valid: match !== null,
parts: match?.groups
? {
local: match.groups.local,
domain: match.groups.domain,
}
: undefined,
};
};
// The test code continues...
The test cases include the following items:
- Basic email address validation
- Special characters in
atext(!#$%\u0026'*+/=?^_`{|}~-) - Quoted-string support (RFC 5322 only)
- Comment support (RFC 5322 only)
- Domain-literal (IP address) support
- DNS constraint validation (RFC 5321)
- Length limit validation
- Error case validation (consecutive dots, invalid domains, etc.)
7. Comparison of RFC 5321 and RFC 5322
The main differences between the two implemented regular expressions are summarized below:
| Item | RFC 5321 | RFC 5322 |
|---|---|---|
| Format of local-part |
dot-atom + quoted-string
|
dot-atom + quoted-string + mixed format |
| Domain constraints | DNS-compliant (alphanumeric and hyphens only) | Syntactically flexible (but DNS constraints recommended in practice) |
| Comments | Not permitted | Can be placed before/after @ and around dots |
| Maximum length | 256 characters | 998 characters (recommended) |
8. Summary
In this article, we incrementally constructed regular expressions for email addresses conforming to RFC 5321 and RFC 5322.
To reiterate, when performing email address validation in actual applications, we recommend using the HTML specification regular expression introduced at the beginning of this article.
/^[a-zA-Z0-9.!#$%\u0026'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
The RFC-compliant regular expressions presented in this article are intended solely for understanding and learning the RFC specifications. In production environments, it is important to choose an appropriate validation method based on the use case.
Discussion