JavaScript - 与格式错误的JSON中的正则表达式匹配的键

时间:2015-11-24 11:58:07

标签: javascript json regex steam

我正在尝试修复一些有时由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' 
        }
    }
}

1 个答案:

答案 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