简体   繁体   中英

How can I create a JavaScript regex for finding email addresses in strings?

I have a logic app which is triggered by emails in an inbox. It is all working, except for some emails are getting through when I don't want them. Or rather an email signature with an image description of image001.png@01D766B1.7C184990 is getting through. I think it might be my regex that is allowing it, but I am not very good with regex.

Here is the code I have so far:

 var reg = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\\.[a-zA-Z0-9_-]+)/gi; var emailData = " \\n\\n'Phonenumber2@test.com'\\n\\n \\n\\n DevOps\\n[cid:image001.png@01D766B1.7C184990]\\n\\n "; //Matching email signatures var matches = emailData .match(reg); console.log(matches);

I need the regex to return a list of any email addresses, but they need to be fully formed. Unlike the one mentioned above which is missing the .com (or .org etc).

Your regex (allowing everything which has an @ and a . )

 const regex = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\\.[a-zA-Z0-9_-]+)/gm; const str = `Phonenumber2@test.com Phonenumber2@test.info image001.png@01D766B1.7C184990 Phonenumber2@test.org`; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } console.log(m[0]); }

#1 No numbers allowed after last .

 const regex = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\\.[a-zA-Z_-]+)/gm; const str = `Phonenumber2@test.com Phonenumber2@test.info image001.png@01D766B1.7C184990 Phonenumber2@test.org`; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } console.log(m[0]); }

#2 Restrict characters after last . to be min 2 and max 7 characters {2,7}$

 const regex = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\\.[a-zA-Z0-9_-]{2,7}$)/gm; const str = `Phonenumber2@test.com Phonenumber2@test.info image001.png@01D766B1.7C184990 Phonenumber2@test.org`; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } console.log(m[0]); }

#3 Define a list of possible top-level domain names like (com|org|info)

 const regex = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\\.(com|org|info)$)/gm; const str = `Phonenumber2@test.com Phonenumber2@test.info image001.png@01D766B1.7C184990 Phonenumber2@test.org`; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } console.log(m[0]) }

have a look at - https://ihateregex.io/expr/email/

Hate to break it to you but email match via Regex are hard if not impossible.

one way would be matching things with domain name TDN endings ( you can create a group of all tdn and match or just limit the end part of regex - modified regex and filter it out from there onwards.

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