Validate email address with folder using regex in Javascript

I am writing a regular expression to validate an email address in JavaScript.

/^[a-zA-Z]([w-.]{4,29})+@([w-]+.)+[a-zA-Z]{2,3}$/

This is working fine. But there came an additional requirement like " In a GMAIL address we can specify the folder name along with the email id to which the mail will be delivered. For ex. james+office@gmail.com , the mail will be delivered to the folder "office" under james@gmail.com's inbox.

So how can I validate that also in the above regex. The Plus symbol is not mandatory but if + is added it should be in between the other characters before @ symbol .


You can simply add the + character inside of your character class and use a negated match before the @ . Note that I removed the escape sequence from the dot and moved the hyphen as the last character, or else you need to escape it and also implemented the use of the i modifier for case-insensitive matching.

/^[a-z]([w.+-]{4,29})[^+]@([w-]+.)+[a-z]{2,3}$/i

Live Demo


改用这个:

/^((([a-z]|d|[!#$%&'*+-/=?^_`{|}~]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])+(.([a-z]|d|[!#$%&'*+-/=?^_`{|}~]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])+)*)|((x22)((((x20|x09)*(x0dx0a))?(x20|x09)+)?(([x01-x08x0bx0cx0e-x1fx7f]|x21|[x23-x5b]|[x5d-x7e]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([x01-x09x0bx0cx0d-x7f]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))))*(((x20|x09)*(x0dx0a))?(x20|x09)+)?(x22)))@((([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).)+(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).?$/i

I tried this RegEx in the desired scenario you required and it worked fine

^([a-zA-Z0-9_-.+]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([a-zA-Z0-9-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$

Try it in RegEx Validator

Thanks, Hope it helps

链接地址: http://www.djcxy.com/p/16524.html

上一篇: 用java和java scipt进行电子邮件地址的正则表达式验证

下一篇: 在Javascript中使用正则表达式验证包含文件夹的电子邮件地址