Reputation: 302
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 [email protected] 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'[email protected]'\n\n \n\n DevOps\n[cid:[email protected]]\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).
Upvotes: 0
Views: 184
Reputation: 22480
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 = `[email protected]
[email protected]
[email protected]
[email protected]`;
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 = `[email protected]
[email protected]
[email protected]
[email protected]`;
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 = `[email protected]
[email protected]
[email protected]
[email protected]`;
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 = `[email protected]
[email protected]
[email protected]
[email protected]`;
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])
}
Upvotes: 2
Reputation: 43
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.
Upvotes: 0