JavaScript / ECMAScript

Email Local Part (Before @) in JS

Validate just the local part of an email address (the bit before the @).

Try it in the JS tester →

Pattern

regexJS
^[a-zA-Z0-9._%+\-]{1,64}$

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("^[a-zA-Z0-9._%+\\-]{1,64}$", "");
const input = "john.doe";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern works

^ and $ anchor to the full string. [a-zA-Z0-9._%+\-] is the conservative local-part character set (dots, underscores, percent, plus, hyphen for sub-addressing). {1,64} enforces the RFC max length for the local part.

Examples

Input

john.doe

Matches

  • john.doe

Input

user+tag

Matches

  • user+tag

Input

has space

No match

Same pattern, other engines

← Back to Email Local Part (Before @) overview (all engines)