const regex = /[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('[a-z0-9!#$%&\'*+\\\/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&\'*+\\\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
', 'gm')
const str = `frankp@gmail.com
shannon.eee@gmail.com
franks&shannon@gmail.com
frank%ddd@gmail.com
frank@gmail\$.com
frank@gmail_gmail.com
frank@gmail.research.com
joe@ggg.gg.cccc.com
joe..joe@gmail.com
/
https://stackoverflow.com/questions/5174171/fix-regular-expression-for-emails-to-not-allow-consecutive-periods/5174201#5174201
*To avoid matching two consecutive dots you can add a negative lookahead at the beginning of your regular expression:
/^(?!.*\\.{2})[a-z0-9etc...
------------
It will fail to match if there are two consecutive periods anywhere in the string and it doesn't require any other modifications to your original regular expression.
However it seems a bad idea as your regular expression isn't correct in the first place. If you insist on using regular expressions to validate email addresses, try this:
*/
wilson..gmail.com
wilson.gmail.com
^(?!.*\\.{2})/[a-z0-9!#\$%&'*+/=?^_\`{|}~-]+(?:\\.[a-z0-9!#\$%&'*+/=?^_\`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
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++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions