const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
// Email validation function
const validateEmail = (email) => {
return emailRegex.test(email);
}
// Usage example
const email = "example@example.com";
const isValidEmail = validateEmail(email);
if (isValidEmail) {
console.log("Valid email format.");
} else {
console.log("Invalid email format.");
}
In the above code, emailRegex
is the regular expression that represents the email address format. The regular expression is enclosed in /.../
, where ^
represents the start of the string and $
represents the end of the string. [^\s@]+
represents one or more non-space and non-@
characters, and \.
represents the .
character.
The validateEmail
function is used to check if an email address is valid. It uses the test
function to match the emailRegex
with the provided email address. The result is stored in the isValidEmail
variable, which can be used to determine the validity of the email address.