Apache Common Log Format in JS
Parses Apache Common Log Format entries.
Try it in the JS tester →Pattern
regexJS
^(\S+) (\S+) (\S+) \[([^\]]+)\] "(\S+) (\S+) (\S+)" (\d{3}) (\d+|-)JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("^(\\S+) (\\S+) (\\S+) \\[([^\\]]+)\\] \"(\\S+) (\\S+) (\\S+)\" (\\d{3}) (\\d+|-)", "");
const input = "127.0.0.1 - frank [10/Oct/2023:13:55:36 -0700] \"GET /index.html HTTP/1.0\" 200 2326";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
How the pattern works
Captures 9 fields: host, ident, user, timestamp, method, path, protocol, status code, and response size.
Examples
Input
127.0.0.1 - frank [10/Oct/2023:13:55:36 -0700] "GET /index.html HTTP/1.0" 200 2326Matches
127.0.0.1 - frank [10/Oct/2023:13:55:36 -0700] "GET /index.html HTTP/1.0" 200 2326