Regex to Match JSON Keys
Extract JSON object keys from JSON strings with this regex pattern. Matches double-quoted property names followed by a colon separator. Free tool.
Regular Expression
/"([^"\\]*(?:\\.[^"\\]*)*)"\s*:/g
Token Breakdown
| Token | Description |
|---|---|
| " | Matches the literal character '"' |
| ( | Start of capturing group |
| [^"\\] | Negated character class — matches any character NOT in "\\ |
| * | Matches the preceding element zero or more times (greedy) |
| (?: | Start of non-capturing group |
| \\ | Escaped character '\' |
| . | Matches any character except newline (unless dotAll flag is set) |
| [^"\\] | Negated character class — matches any character NOT in "\\ |
| * | Matches the preceding element zero or more times (greedy) |
| ) | End of group |
| * | Matches the preceding element zero or more times (greedy) |
| ) | End of group |
| " | Matches the literal character '"' |
| \s | Matches any whitespace character (space, tab, newline) |
| * | Matches the preceding element zero or more times (greedy) |
| : | Matches the literal character ':' |
Detailed Explanation
This regex matches JSON object keys, which are double-quoted strings followed by a colon. Here is the token-by-token breakdown:
" — Matches the opening double quote of the JSON key.
( — Opens a capturing group for the key name content.
[^"\]* — Matches zero or more characters that are neither a double quote nor a backslash. This handles the simple case of keys without escape sequences.
(?:\.[^"\]) — A non-capturing group repeated zero or more times for handling escape sequences. \. matches a backslash followed by any character (the escape sequence), and [^"\]* matches any subsequent non-quote, non-backslash characters. This correctly handles keys containing escaped quotes.
) — Closes the capturing group.
" — Matches the closing double quote.
\s* — Matches optional whitespace between the closing quote and the colon.
: — Matches the literal colon that follows JSON keys.
The g flag enables global matching to find all keys in the JSON string. The first capturing group contains the key name without quotes. This pattern correctly handles escaped characters within key names, including escaped quotes, backslashes, and Unicode escape sequences.
This regex is useful for extracting or analyzing JSON structure without a full JSON parser, such as in log analysis, data exploration, or building JSON visualization tools.
Example Test Strings
| Input | Expected |
|---|---|
| "name": "John" | Match |
| "age": 30 | Match |
| name: John | No Match |
| 'key': 'value' | No Match |
| "complex\"key": true | Match |
Try It — Interactive Tester
Match Highlighting(3 matches)
Matches & Capture Groups
30 charsFlags: gMatches: 3Ctrl+Shift+C to copy regex