0

I'm using regex and match to grab all emails in an HTML string, however I'm getting false positives, how can I improve my regex (I'm not very good at it), I'd like to add something like "ends in .com" so I don't grab any false matches.

const emailRegex = /([\s]*)([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*([ ]+|)@([ ]+|)([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,}))([\s]*)/ig;
let emailsInFacebook = bodyHTML.match(emailRegex); 

This gives me an array with false positives like this one:

emails: 
{
  '0': 'badge-silver_es_ES@2x.jpg',
  '2': 'badge-silver_es_ES@2x-150x150.jpg',
  '3': 'info@grupomonico.com'
}
Yom T.
  • 8,760
  • 2
  • 32
  • 49
gabogabans
  • 3,035
  • 5
  • 33
  • 81
  • 1
    Does this answer your question? [How to validate an email address using a regular expression?](https://stackoverflow.com/questions/201323/how-to-validate-an-email-address-using-a-regular-expression) – Brian McCutchon Sep 19 '20 at 16:34
  • 3
    It seems to me that these are all valid email addresses. Consider that `.jpg` could be a TLD (like`.com`) in the future and you don't want to have to change your regex when that happens. Instead of eliminating false positives, you might use a verification email to check that the user entered the correct address. You should be more worried about false negatives, like how your regex doesn't allow foo+bar@example.com even though it's valid. – Brian McCutchon Sep 19 '20 at 16:45

0 Answers0