Skip to content

Instantly share code, notes, and snippets.

@obonyojimmy
Forked from jamischarles/sanitize_json.js
Created November 30, 2017 20:54
Show Gist options
  • Save obonyojimmy/8b7ca6c248d4e622585da8ff90017fb1 to your computer and use it in GitHub Desktop.
Save obonyojimmy/8b7ca6c248d4e622585da8ff90017fb1 to your computer and use it in GitHub Desktop.
JavaScript: Sanitize JSON string before saving, so it can be read again. (Escapes newlines etc)
function sanitizeJSON(unsanitized){
return unsanitized.replace(/\\/g, "\\\\").replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t").replace(/\f/g, "\\f").replace(/"/g,"\\\"").replace(/'/g,"\\\'").replace(/\&/g, "\\&");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment