JavaScript - 转义双引号 [英] JavaScript - Escape double quotes
本文介绍了JavaScript - 转义双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果 JSON 字符串如下,你如何转义双引号?
How do you escape double quotes if the JSON string is the following?
var str = "[{Company: "XYZ",Description: ""TEST""}]"
我想转义值 TEST 中的辅助双引号.
I want to escape the secondary double quotes in value TEST.
我尝试了以下方法,但不起作用.
I have tried the following, but it does not work.
var escapeStr = str.replace(/""/g,'"');
我错过了什么?
推荐答案
应该是:
var str='[{"Company": "XYZ","Description": "\"TEST\""}]';
首先,我将外引号改为单引号,这样它们就不会与内引号发生冲突.然后我在 TEST
周围最里面的引号之前加上反斜杠,以对它们进行转义.我转义了反斜杠,以便按字面意思处理.
First, I changed the outer quotes to single quotes, so they won't conflict with the inner quotes. Then I put backslash before the innermost quotes around TEST
, to escape them. And I escaped the backslash so that it will be treated literally.
您可以使用 JSON 函数获得相同的结果:
You can get the same result with use of a JSON function:
var str=JSON.stringify({Company: "XYZ", Description: '"TEST"'});
这篇关于JavaScript - 转义双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文