Skip to content

Instantly share code, notes, and snippets.

@dimaxweb
Created September 28, 2012 18:49
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 dimaxweb/3801501 to your computer and use it in GitHub Desktop.
Save dimaxweb/3801501 to your computer and use it in GitHub Desktop.
Make string securely embeddable in to JavaScript function call
//escape the text in order to make it secure for embedding in JavaScript.
function jsEscape(content) {
return content.replace(/(['\\])/g, '\\$1')
.replace(/[\f]/g, "\\f")
.replace(/[\b]/g, "\\b")
.replace(/[\n]/g, "\\n")
.replace(/[\t]/g, "\\t")
.replace(/[\r]/g, "\\r")
.replace(/[\u2028]/g, "\\u2028")
.replace(/[\u2029]/g, "\\u2029");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment