如何删除双引号并将其转义为字符串的特定部分

时间:2013-09-06 19:17:58

标签: javascript

我有这个字符串:

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

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

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

如何在JavaScript中执行此操作?

1 个答案:

答案 0 :(得分:2)

replace与正则表达式一起使用:

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

str = str.replace(/""([^"]*)"",/g,'"\\"$1\\"",');