Skip to content

Instantly share code, notes, and snippets.

@davidsheardown
Created November 7, 2018 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidsheardown/d0ac5ac7b527cea3172d284b76aa13b5 to your computer and use it in GitHub Desktop.
Save davidsheardown/d0ac5ac7b527cea3172d284b76aa13b5 to your computer and use it in GitHub Desktop.
Javascript escaping escape characters!
//
// I had a recent issue where I needed to escape carriage returns within a JSON object, or correctly, the JSON string itself.
// You can of course use the JSON stringify function to get a string representation and then you need to do the following
// to escape the "\n" carriage return sequence..
//
var stringifiedJSON = JSON.stringify(jsonData).replace(/\\n/g, "\\\\n");
//
// The above will globally (within the string) replace all \n with \\n which allows JS to store the carriage return.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment