Skip to content
dz / regex-email-validation-javascript
Text · Regex tester

Regex for email validation in JavaScript

The practical pattern is loaded below with test addresses – edit them and see which ones pass.

/regex

The practical pattern

^[^\s@]+@[^\s@]+\.[^\s@]{2,}$ reads: something without spaces or @, an @, a domain, a dot and a top-level part of at least two characters. It accepts every normal address and rejects obvious typos – which is all a form needs. The real test is the confirmation e-mail.

const isEmail = (s) => /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(s.trim());

isEmail('jane@example.com');   // true
isEmail('jane@localhost');     // false – no dot in the domain

What browsers use

For <input type="email">, the HTML standard defines a stricter pattern that checks the characters allowed before the @ and the shape of every domain label:

/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[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])?)*$/

Using the input type gives you this check for free, plus the right keyboard on phones.

Why no regex is "correct"

The full address grammar (RFC 5322) allows quoted local parts, comments and IP-address domains; a regex that covers it is thousands of characters long and still cannot tell whether the mailbox exists. International addresses such as jürgen@beispiel.de pass the practical pattern but fail ASCII-only ones like the browser's. Keep the pattern simple and confirm by sending a mail.

Questions

Should I validate e-mail on the server too?

Yes – client-side checks are for convenience only. Validate again on the server, ideally with your framework's validator, and confirm the address with a link.

Why does the pattern use the m flag here?

Only so that each line of the test text is checked separately. In code you test one address at a time, so ^ and $ without m anchor to the whole string.

Does it work in PHP or C#?

The same pattern works in PCRE2 and .NET – switch the flavor above. PHP also has filter_var($email, FILTER_VALIDATE_EMAIL), .NET has System.Net.Mail.MailAddress.TryCreate.

▸ About this tool

About the Regex Tester

Introduction

Regular expressions are compact, powerful – and easy to get subtly wrong. This tester shows exactly what a pattern matches while you type it: every match highlighted in your text, each capture group with its value and position, the result of a replacement, a plain-English explanation of the pattern, and code you can paste straight into your project. Unlike most online testers, it does not imitate other languages in JavaScript: each flavor runs on its real engine.

Three real engines

  • JavaScript – the regex engine of your own browser (RegExp), with the g i m s u v y flags. It runs locally; nothing is sent anywhere.

  • PCRE2 · PHP – PHP's preg_match, preg_match_all and preg_replace on our server. PCRE2 is also the engine behind many other tools, so it is the best reference for PHP and closely related Perl-style syntax: possessive quantifiers a++, atomic groups, \A \z, (?P<name>…), the x, U and n modifiers.

  • .NET · C# – System.Text.RegularExpressions from .NET 10 on our server, with RegexOptions such as IgnoreCase, Singleline, ExplicitCapture and RightToLeft.

The differences are real: in .NET, named groups are numbered after all unnamed ones, so $2 in a replacement may mean something else than in JavaScript or PHP. Switch the flavor and compare.

How to use it

  1. Type a pattern between the slashes and toggle the flags below it. g finds all matches instead of the first.

  2. Paste the text to search. Matches light up in alternating colours; the Matches list shows each match and its groups – click one to select it in the text.

  3. Tick Replace to try a replacement with group references ($1, ${name}, $<name> depending on the flavor).

  4. Explanation describes the pattern token by token, Code gives a ready-made snippet for JavaScript, PHP or C#, and Reference is a cheat sheet – click a token to insert it.

Short links: develzone.org/regex, /regexp and /re. Pasting a regex literal such as /\d+/g into the search box on any page opens it here. Share copies a link with the pattern, flags and test text.

Catastrophic backtracking

Some patterns – typically nested quantifiers such as (a+)+$ – take exponentially long on text that almost matches. A server with such a regex can be brought down by one request (a "ReDoS"). The tester stops every run after about two seconds (JavaScript and .NET) or when PCRE's backtracking limit is reached, and tells you why. The fix is usually a more specific pattern, an atomic group (?>…) or a possessive quantifier a++ where the flavor supports them.

Good to know

  • Match positions are counted in UTF-16 code units, as JavaScript does – an emoji counts as two.

  • Use u in JavaScript and PCRE2 for correct Unicode handling and \p{L}-style classes.

  • Validating e-mail addresses or URLs with a regex is always a compromise – the Common patterns menu has practical versions with test text that shows where they stop.

  • Need to compare two texts instead? Try the text diff.

Privacy

The JavaScript flavor runs entirely in your browser. For PCRE2 and .NET, the pattern and test text are sent to our server for each test, processed in memory and not logged or stored. Do not paste secrets you would not want to leave your computer – use the JavaScript flavor for those.

❯ Esc
↑ ↓ select ↵ open Shift ↵ new line