const regex = new RegExp('^(?:(?<network_srcIpv4>(?:[0-9]{1,3}\\.){3}[0-9]{1,3})|(?<network_srcIpv6>[:\\-0-9a-fA-F]+?)|(?<network_srcHost>.+?)) - (?:-|(?<user_username>.+)) \\[(?<time>.*)\\] \\"(?<application_cmd>(?<application_http_method>[A-Z]+)\\s(?:(?<application_proto>.*?)://)?(?<network_fqdn>[^/]*?)(?:\\:(?<network_dstPort>\\d+))?(?<file_path>/.*?)?(?:\\?(?<application_http_queryString>.*?))?(?: HTTP/(?<application_http_version>[0-9\\.]+)?))\\" (?<application_http_status>\\d+) (?<application_len>\\d+) (?:"(?:-|(?<application_http_referrer>.*))")? (?:"(?:-|(?<application_http_userAgent>.*))")$', 'gm')
const str = `218.1.111.50 - - [13/Mar/2005:10:36:12 -0500] "GET http://www.yahoo.com/ HTTP/1.1" 403 2898 "-" "Mozilla/4.0 (compatible; MSIE 4.01; Windows 95)"
24.23.244.160 - - [13/Mar/2005:13:48:40 -0500] "GET /scripts/..%25%35%63../winnt/system32/cmd.exe?/c+dir HTTP/1.0" 404 1041 "-" "-"`;
// 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