Created
September 28, 2012 18:49
-
-
Save dimaxweb/3801501 to your computer and use it in GitHub Desktop.
Make string securely embeddable in to JavaScript function call
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
//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