JSON Log Line (Single-Line Object) in JS
Match a single-line JSON object — typical of structured logging from services like slog, Bunyan, or Pino.
Try it in the JS tester →Pattern
regexJS
^\{(?:[^{}]|\{[^{}]*\})*\}$JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("^\\{(?:[^{}]|\\{[^{}]*\\})*\\}$", "");
const input = "{\"level\":\"info\",\"msg\":\"started\",\"port\":8080}";
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
^\{ anchors to an opening brace at start. (?:[^{}]|\{[^{}]*\})* matches any non-brace chars or one level of nested braces (so `{"a":{"b":1}}` matches but deeper nesting may not). \}$ anchors to the closing brace at end. Quick filter for log-line shape; pair with JSON.parse to validate fully.
Examples
Input
{"level":"info","msg":"started","port":8080}Matches
{"level":"info","msg":"started","port":8080}
Input
{"event":"login","user":{"id":42}}Matches
{"event":"login","user":{"id":42}}
Input
plain text log lineNo match
—