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

TokenDescription
"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 '"'
\sMatches 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

InputExpected
"name": "John"Match
"age": 30Match
name: JohnNo Match
'key': 'value'No Match
"complex\"key": trueMatch

Try It — Interactive Tester

//g
gimsuy

Match Highlighting(3 matches)

"name": "John" "age": 30 name: John 'key': 'value' "complex\"key": true

Matches & Capture Groups

#1"name":index 0
Group 1:name
#2"age":index 15
Group 1:age
#3"complex\"key":index 51
Group 1:complex\"key
Pattern: 30 charsFlags: gMatches: 3

Ctrl+Shift+C to copy regex

Customize this pattern →