Apache/Nginx Combined Log Format Regex for JavaScript
/^(\S+) \S+ (\S+) \[([^\]]+)\] "([A-Z]+) ([^"]+) HTTP/[\d.]+" ([1-5]\d{2}) (\d+|-)(?:\s"([^"]*)" "([^"]*)")?/What this pattern does
This page provides a comprehensive, battle-tested regular expression for matching apache/nginx combined log format, ported and verified for JavaScript. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your JavaScript project — whether you're validating in an Express middleware, a Next.js API route, or a client-side form.
Javascript Implementation
// Apache/Nginx Combined Log Format
// ReDoS-safe | RegexVault — Dev & Systems > Log Parsing
const apachenginxCombinedLogFormatRegex = /^(\S+) \S+ (\S+) \[([^\]]+)\] "([A-Z]+) ([^"]+) HTTP\/[\d.]+" ([1-5]\d{2}) (\d+|-)(?:\s"([^"]*)" "([^"]*)")?/;
function validateApachenginxCombinedLogFormat(input: string): boolean {
return apachenginxCombinedLogFormatRegex.test(input);
}
// Example
console.log(validateApachenginxCombinedLogFormat("192.168.1.1 - frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326 "http://www.example.com/start.html" "Mozilla/5.0"")); // trueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
192.168.1.1 - frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326 "http://www.example.com/start.html" "Mozilla/5.0" | not a log line |
10.0.0.1 - - [01/Jan/2024:00:00:00 +0000] "POST /api/v1/users HTTP/1.1" 201 450 | 192.168.1.1 [date] GET / 200 |
| — | plain text log |
When to use this pattern
This pattern is drawn from the Dev & Systems > Log Parsing category and carries a ReDoS-safe certification. That matters for JavaScript developers because especially critical in long-running Node.js event loops where a ReDoS vulnerability can block the entire process. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
Log lines with escaped quotes (\" inside request field) will break naive parsers. Always handle escaped quotes inside quoted fields.
Technical Notes
Groups: 1=client IP, 2=auth user, 3=datetime, 4=method, 5=path, 6=status, 7=bytes, 8=referer, 9=user-agent. The - placeholder indicates missing values.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern