我有这个字符串:
{
"QueryString": null,
"ProjectName": ""24"",
"SeasonName": "",
"MemberName": "",
"CompanyName": "",
"CompanyRole": 0,
"CompanyRoles": "",
"Year": ""
}
我希望使用替换函数使它看起来像这样(对于所有在它们中都有双引号的值 - 在这种情况下,只有属性ProjectName的值具有它):
{
"QueryString": null,
"ProjectName": "\"24\"",
"SeasonName": "",
"MemberName": "",
"CompanyName": "",
"CompanyRole": 0,
"CompanyRoles": "",
"Year": ""
}
如何在JavaScript中执行此操作?
答案 0 :(得分:2)
将replace与正则表达式一起使用:
var str = '{\
"QueryString": null,\
"ProjectName": ""24"",\
"SeasonName": "",\
"MemberName": "",\
"CompanyName": "",\
"CompanyRole": 0,\
"CompanyRoles": "",\
"Year": ""\
}';
str = str.replace(/""([^"]*)"",/g,'"\\"$1\\"",');