const regex = /(?:\s*from\s(?:(?:([^\(\n]*?)\s)|(\[[^\]]*\]))?(?:\((?:(?:((?!HELO|EHLO)[^\s]*)|(?:(?:EHLO)|(?:HELO)))?(?:[\s\w\.]*(?:(?:\[([^\]]*)\])|([^\)\s]*))\))))?(?:\{.*\})?)?
\s*(?:(?:client\s)?[\(\{\[]+([^\)\}\]]*)[\)\}\]]+\;?(?:\{.*\})?)?
(?:[\s\n]*)
(?:\s*(?:by(?:(?:.*?)(?:\;|(?:[\r\n]+))))(?!.*with))?
(?:\s*(?:by\s[^\;]*\)?)(?=.*with)\s*)?
(?:with\s*(?:((?:[^\n;]*))(?:.*\;)?))?
(?:[\s\n]*)?
(?:\(?envelope-from\s*([^\n]*)\;?\)?)?
(?:\s*id[^;]*\;?)?
(?:[\s\n]*)?
(?:for\s([^\;]*)\;)?
(?:[\s\n]*)?
(?:\(.*\)\;)?(?:[\s\n])?
([^\(]+)?
(?:\(?envelope-from\s*([^\n]*)\;?\)?)?/m;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?:\\s*from\\s(?:(?:([^\\(\\n]*?)\\s)|(\\[[^\\]]*\\]))?(?:\\((?:(?:((?!HELO|EHLO)[^\\s]*)|(?:(?:EHLO)|(?:HELO)))?(?:[\\s\\w\\.]*(?:(?:\\[([^\\]]*)\\])|([^\\)\\s]*))\\))))?(?:\\{.*\\})?)?
\\s*(?:(?:client\\s)?[\\(\\{\\[]+([^\\)\\}\\]]*)[\\)\\}\\]]+\\;?(?:\\{.*\\})?)?
(?:[\\s\\n]*)
(?:\\s*(?:by(?:(?:.*?)(?:\\;|(?:[\\r\\n]+))))(?!.*with))?
(?:\\s*(?:by\\s[^\\;]*\\)?)(?=.*with)\\s*)?
(?:with\\s*(?:((?:[^\\n;]*))(?:.*\\;)?))?
(?:[\\s\\n]*)?
(?:\\(?envelope-from\\s*([^\\n]*)\\;?\\)?)?
(?:\\s*id[^;]*\\;?)?
(?:[\\s\\n]*)?
(?:for\\s([^\\;]*)\\;)?
(?:[\\s\\n]*)?
(?:\\(.*\\)\\;)?(?:[\\s\\n])?
([^\\(]+)?
(?:\\(?envelope-from\\s*([^\\n]*)\\;?\\)?)?', 'm')
const str = `from qzienfhr ([182.109.196.197]) by mail.oucu.edu.eg
with SMTP (Code-Crafters Ability Mail Server 2012);
Mon, 03 Dec 2018 12:42:56 +0200`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
if ((m = regex.exec(str)) !== null) {
// 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