1 month ago

False positive with email regex

Posted 1 month ago by Gabotronix

Hi everybody, 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:

  '0': '[email protected]',
  '2': '[email protected]',
  '3': '[email protected]'

Please sign in or create an account to participate in this conversation.