const regex = /^(?P<timestamp>[^\s]+)\s*(?P<elb>[^\s]+)\s*(?P<client_addr>[^:]+):\s*(?P<client_port>[^\s]+)\s*(?P<target_addr>[^:]+):\s*(?P<target_port>[^\s]+)\s*(?P<request_processing_time>[^\s]+)\s*(?P<backend_processing_time>[^\s]+)\s*(?P<response_processing_time>[^\s]+)\s*(?P<elb_status_code>[^\s]+)\s*(?P<backend_status_code>[^\s]+)\s*(?P<received_bytes>[^\s]+)\s*(?P<sent_bytes>[^\s]+)\s*"(?P<request>[^\"]+)"\s*(?P<user_agent>[^\s]+)\s*(?P<ssl_cipher>[^\s]+)\s*(?P<ssl_protocol>[^\s]+)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?P<timestamp>[^\\s]+)\\s*(?P<elb>[^\\s]+)\\s*(?P<client_addr>[^:]+):\\s*(?P<client_port>[^\\s]+)\\s*(?P<target_addr>[^:]+):\\s*(?P<target_port>[^\\s]+)\\s*(?P<request_processing_time>[^\\s]+)\\s*(?P<backend_processing_time>[^\\s]+)\\s*(?P<response_processing_time>[^\\s]+)\\s*(?P<elb_status_code>[^\\s]+)\\s*(?P<backend_status_code>[^\\s]+)\\s*(?P<received_bytes>[^\\s]+)\\s*(?P<sent_bytes>[^\\s]+)\\s*"(?P<request>[^\\"]+)"\\s*(?P<user_agent>[^\\s]+)\\s*(?P<ssl_cipher>[^\\s]+)\\s*(?P<ssl_protocol>[^\\s]+)', 'gm')
const str = `2015-05-13T23:39:43.945958Z my-loadbalancer 192.168.131.39:2817 10.0.0.1:80 0.000086 0.001048 0.001337 200 200 0 57 "GET https://www.example.com:443/ HTTP/1.1" "curl/7.38.0" DHE-RSA-AES128-SHA TLSv1.2
2015-05-13T23:39:43.945958Z my-loadbalancer 192.168.131.39:2817 10.0.0.1:80 0.001069 0.000028 0.000041 - - 82 305 "- - - " "-" - -
2015-05-13T23:39:43.945958Z my-loadbalancer 192.168.131.39:2817 10.0.0.1:80 0.001065 0.000015 0.000023 - - 57 502 "- - - " "-" ECDHE-ECDSA-AES128-GCM-SHA256 TLSv1.2`;
// 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