Javascript转义双引号 [英] Javascript Escape Double Quotes
本文介绍了Javascript转义双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果JSON字符串是这样,如何转义双引号:
How do you escape double quotes if the JSON string is this:
var str = "[{Company: "XYZ",Description: ""TEST""}]"
我想对值TEST中的第二个双引号进行转义.
I want to escape the secondary double quotes in value TEST.
我已经尝试过了,但是没有用.
I have tried this 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屋!
查看全文