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 [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._-][email protected][a-zA-Z0-9._-]+.[a-zA-Z0-9_-]+)/gi;
var emailData = " nn'[email protected]'nn nn  DevOpsn[cid:[email protected]]nn ";
    
//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).

Answer

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

const regex = /([a-zA-Z0-9._-][email protected][a-zA-Z0-9._-]+.[a-zA-Z0-9_-]+)/gm;
const str = `[email protected]
[email protected]
i[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._-][email protected][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._-][email protected][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._-][email protected][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])
}