0

According to what I've read ( this article for instance) you need to escape \ and " when sending HTML as JSON.

I wanted to use unescape(), but it is deprecated since JS 1.5.

"Use decodeURI() or decodeURIComponent() instead", w3schools suggests. But decodeURI() or decodeURIComponent() do not escape \ and ".

The response I'm getting looks something like:

"html": {
    "searchresult": "<div class=\\\"item\\\">\n\t\t\t<article class=\\\"unit\\\">\n\t\t\t\t<a href=\\\"page2.html\\\">Link<\\/a>\n\n\t\t\t\t\n\t\t\t<\\/article>\n\t\t<\\/div>"
}

I am then saving the following as var markup:

return $('<textarea/>').html( response ).val();

and am finally left with this semi working html

<div class=\"item\"> <article class=\"unit\"> <a href=\"page2.html\">Link<\/a> <\/article> <\/div>

I now need to remove all \ before ", and /\ for it work. Guess removing the whitespace in between the tags could be a bonus to.

I have tried the following to remove \", which works great.

markup.replace('\\"', '"');

But I can't figure out how to also remove the /\ (plus .replace(blah).replace(blah) doesn't feel right). This article gave me a hint, but I still feel quite lost in the magic world of Regexp.

Any help much appreciated!

Community
  • 1
  • 1
patad
  • 9,364
  • 11
  • 38
  • 44
  • 1
    Don't do that. You should use a JSON seralizer. – SLaks Nov 27 '14 at 18:08
  • looks like a duplicate to me: http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript – ymz Nov 27 '14 at 18:12

2 Answers2

2

You haven't said where your data is coming from, but I'm going to assume at some point you have a valid HTML string (which apparently contains newlines and tabs), and apparently you're trying to make that the searchresult property of an html object inside an object, in JSON. Here's how you do that:

var json = JSON.stringify({html: yourString});

Example:

var yourString =
    '<div class="item">\n\t\t\t<article class="unit">\n\t\t\t\t<a href="page2.html">Link</a>\n\n\t\t\t\t\n\t\t\t</article>\n\t\t</div>';
var json = JSON.stringify({html: yourString});
snippet.log("The JSON:");
snippet.log(json);
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • First the json was sent as raw html, picked up from the page: `{html: "
    "}; ` then we read that \ and " should be escaped, as mentioned in the article I've linked to. So now we are escaping \ and " manually.
    – patad Nov 27 '14 at 18:19
  • Thanks a lot for your quick response! But I don't see that the output is valid HTML? Should I just not worry about escaping \ and " instead as this article suggests? http://www.thorntech.com/2012/07/4-things-you-must-do-when-putting-html-in-json/ – patad Nov 27 '14 at 18:21
  • @patad: You don't manually escape them; instead, you use a proper serializer. The result above is valid JSON that, when deserialized, provides an object with a property called `searchresult` whose value is a string containing valid HTML. – T.J. Crowder Nov 27 '14 at 18:23
  • @Crowder: I have updated my question to clarify what I meant. Could you please re-read it to see if your answer needs any update? Thanks a lot! – patad Nov 28 '14 at 10:34
  • @patad: It's not a good idea to completely change your question like that. Instead, change it back and post a new question. – T.J. Crowder Nov 28 '14 at 10:38
  • I see, sorry about that. However I can't find a way to revert it as there is no 'rollback' link and nothing happens when switching revision. If anyone knows how, please feel free to do so. – patad Nov 28 '14 at 12:55
  • @patad: If you click the "edited XX minutes ago" link, that lists revisions, and there's a "rollback" link next to earlier ones. I've done it for you. – T.J. Crowder Nov 28 '14 at 13:01
2

If you want to change \" to " over and entire string.

markup.toString().replace(/\\"/g,'"');
Michael Nelles
  • 5,426
  • 8
  • 41
  • 57
  • ` 'asdfasdf\"sdfasdfasfsa\"asdfasdf'.replace(/\\"/g,'"'); ` returns ` asdfasdf\"sdfasdfasfsa\"asdfasdf" ` so all ` \" ` are not getting replaced. – chendu Oct 12 '22 at 15:30