简体   繁体   中英

Replace single backslash escape with double backslash in JavaScript

I'm using json result from the Bing Search API. In the result, the double quotes is escaped by a single backslash. Javascript however, doesn't accept this. It requires me to escape the double quotes using double backslash. So, my question is that how do I replace the single backslash with the double backslash. For example, a part of the json code is like this

"Description":"LONDON Britain should stay in the EU \"warts and all\", the opposition Labour leader will say on Thursday..."

I would like it be like this

"Description":"LONDON Britain should stay in the EU \\"warts and all\\", the opposition Labour leader will say on Thursday..."

I tried the following solution

json = '"Description":"LONDON Britain should stay in the EU \"warts and all\", the opposition Labour leader will say on Thursday..."';
dfe = JSON.stringify(json);
dfe = dfe.replace(/\\"/g,'\\\\"');

However, it didn't work. It replaced all the backslashes before all double quotes. It went from this...

\"Description\":\"LONDON Britain should stay in the EU \"warts and all\", the opposition Labour leader will say on Thursday...\"

...to this

\\"Description\\":\\"LONDON Britain should stay in the EU \\"warts and all\\", the opposition Labour leader will say on Thursday...\\"

Can anybody tell me how to replace \\" with \\\\" ?

Edit: What I want to do is this

<p id="demo"></p>
var json = '{"d":{"results":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/News?Query=\u0027britain\u0027&$skip=1&$top=1","type":"NewsResult"},"ID":"f1c27ae7-bf16-4741-a789-897f4878c2e1","Title":"Britain should stay in EU \u0027warts and all\u0027 - Corbyn | Reuters","Url":"http://www.firstpost.com/world/britain-should-stay-in-eu-warts-and-all-corbyn-reuters-2728514.html","Source":"Firstpost","Description":"LONDON Britain should stay in the EU \"warts and all\", the opposition Labour leader will say on Thursday, making his first big intervention in the referendum campaign as he seeks to counter criticism he is not doing enough to persuade his voters to back the ...","Date":"2016-04-14T05:10:45Z"}],"__next":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/News?Query=\u0027britain\u0027&$skip=10&$top=10"}}';

obj = JSON.parse(json);
document.getElementById("demo").innerHTML = obj.d.results[0].Title;

那这个呢?

JSON.stringify({"Description":"LONDON Britain should stay in the EU \"warts and all\", the opposition Labour leader will say on Thursday..."}).replace(/\\/g, "\\\\")

Instead of

var json = '{"d":{"results":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/News?Query=\u0027britain\u0027&$skip=1&$top=1","type":"NewsResult"},"ID":"f1c27ae7-bf16-4741-a789-897f4878c2e1","Title":"Britain should stay in EU \u0027warts and all\u0027 - Corbyn | Reuters","Url":"http://www.firstpost.com/world/britain-should-stay-in-eu-warts-and-all-corbyn-reuters-2728514.html","Source":"Firstpost","Description":"LONDON Britain should stay in the EU \"warts and all\", the opposition Labour leader will say on Thursday, making his first big intervention in the referendum campaign as he seeks to counter criticism he is not doing enough to persuade his voters to back the ...","Date":"2016-04-14T05:10:45Z"}],"__next":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/News?Query=\u0027britain\u0027&$skip=10&$top=10"}}';

obj = JSON.parse(json);

Try this:

var json = {"d":{"results":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/News?Query=\u0027britain\u0027&$skip=1&$top=1","type":"NewsResult"},"ID":"f1c27ae7-bf16-4741-a789-897f4878c2e1","Title":"Britain should stay in EU \u0027warts and all\u0027 - Corbyn | Reuters","Url":"http://www.firstpost.com/world/britain-should-stay-in-eu-warts-and-all-corbyn-reuters-2728514.html","Source":"Firstpost","Description":"LONDON Britain should stay in the EU \"warts and all\", the opposition Labour leader will say on Thursday, making his first big intervention in the referendum campaign as he seeks to counter criticism he is not doing enough to persuade his voters to back the ...","Date":"2016-04-14T05:10:45Z"}],"__next":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/News?Query=\u0027britain\u0027&$skip=10&$top=10"}};

It will be JSON object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM