简体   繁体   中英

How can I modify regular expression to validate domain properly?

I found the following regex:

/[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/

on this site: http://www.regular-expressions.info/email.html

and it works great (matches 99.99% of actual emails), with one exception. It allows you to append anything you want after the domain.

example:

test does not match
test@domain does not match
test@domain.com does match
test@domain.co.uk does match
test@domain.comdsaf#()%@dsf,25ljsafdlfkjj&45234^\/3258afsd still matches

I also only want to validate a single email at a time, so the following string should not match, despite being valid in an email client: test@domain.com;test2@domain.com

This is being used by javascript.

/[a-z0-9!#$%&'*+\\/=?^_ {|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_ {|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/

I just added in the dollar sign at the end, indicating that you are expecting the end of the input at that point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM