JavaScript / ECMAScript

JSON Key-Value Pair (Simple) in JS

Extract simple `"key": value` pairs from JSON-ish text (strings, numbers, booleans, null).

Try it in the JS tester →

Pattern

regexJS
"([^"\\]+)"\s*:\s*("[^"\\]*"|-?\d+(?:\.\d+)?|true|false|null)   (flags: g)

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("\"([^\"\\\\]+)\"\\s*:\\s*(\"[^\"\\\\]*\"|-?\\d+(?:\\.\\d+)?|true|false|null)", "g");
const input = "{\"name\": \"alice\", \"age\": 30, \"active\": true}";
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 the key — non-quote, non-backslash chars (so escaped quotes break the match — by design, this is a quick parser, not a strict one). \s*:\s* matches the separator. The value group covers strings, signed integers/decimals, true, false, and null. Use a real JSON parser for production!

Examples

Input

{"name": "alice", "age": 30, "active": true}

Matches

  • "name": "alice"
  • "age": 30
  • "active": true

Input

"timeout": 5000, "retries": null

Matches

  • "timeout": 5000
  • "retries": null

Input

no json here

No match

Same pattern, other engines

← Back to JSON Key-Value Pair (Simple) overview (all engines)