我正在寻找javascript正则表达式来替换没有前反斜杠的引号。
例如:
'"'.replace(xxx, yyy); -> '\"'
'\"'.replace(xxx, yyy); -> '\"'
'\\"'.replace(xxx, yyy); -> '\\\"'
目前,我做了以下工作,但我相信有更好的方法。
content = content.replace(/"/g, '\\"');
content = content.replace(/\\\\"/g, '\\"');
答案 0 :(得分:1)
JSON.stringify('abc " def')
返回
"abc \" \" def"
答案 1 :(得分:1)
据我所知,您只想替换那些没有使用反斜杠字符的引号。为此,您可以在regex
下方使用
var str = 'this"quote but not \"this one';
console.log(str.replace(/(([^\\])(["]))/g, "$2\\$3"));

答案 2 :(得分:0)
如果您要将所有'"'
替换为'\"'
,
然后
var replacedString = 'string with " " quotes'.replace(/"/g,'\\\"');
应该有用。