const regex = /^(?<time>[^ ]* {1,2}[^ ]* [^ ]*) (?<ident>[a-zA-Z0-9_\/\.\-]*)(?:\[(?<pid>[0-9]+)\])?(?:[^\:]*\:)? \[(?<time_server>[^\]]*)\] (?<host>[^ ]*) - (?<user>[^ ]*)\\"(?<method>\S+)(?: +(?<path>[^\"]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*) \\"(?<referer>[^\"]*)\\" \\"(?<agent>[^\"]*)\\"rt=(?<request_time>[^ ]*) uct=\\"(?<upstream_connect_time>[^ ]*)\\" uht=\\"(?<upstream_header_time>[^ ]*)\\" urt=\\"(?<upstream_response_time>[^ ]*)\\" uaddr=\\"(?<upstream_addr>[^ ]*)\\" x-forwarded-for=\\"*(?<http_x_forwarded_for>.*)\\"$/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?<time>[^ ]* {1,2}[^ ]* [^ ]*) (?<ident>[a-zA-Z0-9_\\\/\\.\\-]*)(?:\\[(?<pid>[0-9]+)\\])?(?:[^\\:]*\\:)? \\[(?<time_server>[^\\]]*)\\] (?<host>[^ ]*) - (?<user>[^ ]*)\\\\"(?<method>\\S+)(?: +(?<path>[^\\"]*) +\\S*)?" (?<code>[^ ]*) (?<size>[^ ]*) \\\\"(?<referer>[^\\"]*)\\\\" \\\\"(?<agent>[^\\"]*)\\\\"rt=(?<request_time>[^ ]*) uct=\\\\"(?<upstream_connect_time>[^ ]*)\\\\" uht=\\\\"(?<upstream_header_time>[^ ]*)\\\\" urt=\\\\"(?<upstream_response_time>[^ ]*)\\\\" uaddr=\\\\"(?<upstream_addr>[^ ]*)\\\\" x-forwarded-for=\\\\"*(?<http_x_forwarded_for>.*)\\\\"$', 'g')
const str = `Jun 26 06:53:39 00f628825635[3156]: [26/Jun/2018:06:53:39 +0000] 10.66.44.27 - -\\"GET /health_check HTTP/1.1\\" 200 0 \\"-\\" \\"ELB-HealthChecker/1.0\\"rt=0.000 uct=\\"-\\" uht=\\"-\\" urt=\\"-\\" uaddr=\\"-\\" x-forwarded-for=\\"-\\"`;
// 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