1
votes

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
[email protected] does match
[email protected] does match
[email protected]#()%@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: [email protected];[email protected]

This is being used by javascript.

1
What if you add ^ on the beggining and $ at the end ?DontVoteMeDown
This is how I validate emails: send an email, wait for confirmation; email validated.elclanrs
RFC 822 email addresses are not good candidates for regular expression matching. See ex-parrot.com/pdw/Mail-RFC822-Address.html for a more comprehensive (and accurate) expression.Todd A. Jacobs
Your regex matches correctly this part highlighted [email protected]#()%@dsf,25ljsafdlfkjj&45234^\/3258afsd. It does not match the un-highlighted part. I don't know what you mean by it still matches. You are trying to match the valid email aren't you?user557597
@sln you are correct. what i was looking for was for the entire string to match, not just a portion of it. DontVoteMeDown made a suggestion that makes total sense now that I see it.Edward Talcott

1 Answers

0
votes

/[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.