Created
November 7, 2018 06:36
-
-
Save davidsheardown/d0ac5ac7b527cea3172d284b76aa13b5 to your computer and use it in GitHub Desktop.
Javascript escaping escape characters!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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