1

我有这个字符串:

{
    "QueryString": null,
    "ProjectName": ""24"",
    "SeasonName": "",
    "MemberName": "",
    "CompanyName": "",
    "CompanyRole": 0,
    "CompanyRoles": "",
    "Year": ""
}

我想使用替换函数使它看起来像这样(对于所有包含双引号的值 - 在这种情况下,只有属性 ProjectName 的值具有它):

{
    "QueryString": null,
    "ProjectName": "\"24\"",
    "SeasonName": "",
    "MemberName": "",
    "CompanyName": "",
    "CompanyRole": 0,
    "CompanyRoles": "",
    "Year": ""
}

我该如何在 JavaScript 中执行此操作?

4

1 回答 1

2

使用正则表达式替换:

var str = '{\
    "QueryString": null,\
    "ProjectName": ""24"",\
    "SeasonName": "",\
    "MemberName": "",\
    "CompanyName": "",\
    "CompanyRole": 0,\
    "CompanyRoles": "",\
    "Year": ""\
}';

str = str.replace(/""([^"]*)"",/g,'"\\"$1\\"",');
于 2013-09-06T19:20:51.190 回答