Regex for JSON Key
Regex Pattern
"([^"\\]*(?:\\.[^"\\]*)*)"\s*:JSON object key (quoted string followed by colon)
Quick Answer
The regex pattern for json key is `"([^"\\]*(?:\\.[^"\\]*)*)"\s*:`. JSON object key (quoted string followed by colon). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| "name": "value" | ✓ Matches |
| "key": 123 | ✓ Matches |
| "nested-key": {} | ✓ Matches |
| key: value | ✗ No match |
| 'key': value | ✗ No match |
| unquoted: true | ✗ No match |
Code Examples
javascript
const regex = /"([^"\\]*(?:\\.[^"\\]*)*)"\s*:/; const isValid = regex.test(value);
python
import re
pattern = r'"([^"\\]*(?:\\.[^"\\]*)*)"\s*:'
if re.match(pattern, value):
print("valid")ruby
pattern = /"([^"\\]*(?:\\.[^"\\]*)*)"\s*:/ if value =~ pattern puts "valid" end
php
if (preg_match('/"([^"\\]*(?:\\.[^"\\]*)*)"\s*:/', $value)) {
echo "valid";
}java
String pattern = ""([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"\\s*:"; boolean isValid = value.matches(pattern);
Frequently Asked Questions
Related Regex Patterns
JavaScript Variable Name
Valid JavaScript variable/identifier name
Python Variable Name
Valid Python identifier name
CSS Class Name
Valid CSS class name (starts with letter, underscore, or hyphen)
CSS ID Selector
CSS ID selector starting with #
Semantic Version (Strict)
Strict semantic versioning (MAJOR.MINOR.PATCH)
SemVer with Pre-release
Full semantic version with optional pre-release and build metadata