Sometimes, we want to do email validation with JavaScript regular expression.
In this article, we’ll look at how to do email validation with JavaScript regular expression.
How to do email validation with JavaScript regular expression?
To do email validation with JavaScript regular expression, we use the /^w+@[a-zA-Z_]+?.[a-zA-Z]{2,3}$/
regex.
For instance, we write
const pattern = /^w+@[a-zA-Z_]+?.[a-zA-Z]{2,3}$/;
to assign the regex to pattern
.
We use `w+to match any word before the
@`.
[a-zA-Z_]+?.[a-zA-Z]{2,3}
matches the hostname part of the email address.
Conclusion
To do email validation with JavaScript regular expression, we use the /^w+@[a-zA-Z_]+?.[a-zA-Z]{2,3}$/
regex.