我正在尝试修复一些有时由Steam发送的JSON代码,而不是正确的代码。我遇到的主要问题是钥匙。关于修复类似问题有stack topic,但并不完美。匹配/(['"])?([a-zA-Z0-9_]+)(['"])?:/g
的密钥也匹配http链接或内联css。
是否可以匹配不在/(['"])?([a-zA-Z0-9_]+)(['"])?:/g
匹配的字符串中的([\'](?:.)*[\'])
(这个匹配值)?
示例格式错误的JSON:
{
app_data:
{ '123':
{
appid: 123,
name: 'some game',
icon: 'http://example.com',
test: 'some text with link http://steamcommunity.com don't match it please',
sample: 'blah blah background: red; blah blah'
}
}
}
答案 0 :(得分:-1)
试试这个
['"]?([a-zA-Z0-9_]+)['"]?:[\s\n]
演示:https://regex101.com/r/mM3dP2/4
编辑
对于css和Json键
[{,;]\s*['"]?([-\w]+)['"]?:[\s\n]
演示:https://regex101.com/r/mM3dP2/6
@SlashmanX