.replace(/("(\\[^]|[^\\"])*"(?!\s*:))|"((\\[^]|[^\\"])*)"(?=\s*:)/g, '$1$3')
或者
function unquoteKeys(json) {
return json.replace(/"(\\[^]|[^\\"])*"\s*:?/g, function (match) {
if (/:$/.test(match)) return match.replace(/^"|"(?=\s*:$)/g, '');
else return match;
}
}
这种类型的函数几乎可以用有效的 JSON 来做任何事情。
function modifyJson(json) {
return json.replace(/"(\\[^]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(\.\d+)?([eE][-+]?\d+)?/g, function (match) {
if (/^"/.test(match)) {
if (/:$/.test(match)) {
// match = Key with enclosing quotes & following colon (".*"\s*:)
} else {
// match = String (not key) with enclosing quotes (".*")
}
} else if (/true|false/.test(match)) {
// match = Boolean value (true|false)
} else if (/null/.test(match)) {
// match = Null value (null)
} else {
// match = Number (-?\d+(\.\d+)?([eE][-+]?\d+)?)
}
});
}