Skip to content

Instantly share code, notes, and snippets.

@jamischarles
Created June 25, 2011 17:12
Show Gist options
  • Save jamischarles/1046671 to your computer and use it in GitHub Desktop.
Save jamischarles/1046671 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, "\\&");
}
@yaberkane05
Copy link

Thanks for this !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment