Skip to content

Instantly share code, notes, and snippets.

@englishextra
Last active May 15, 2016 23:11
Show Gist options
  • Save englishextra/3053a4dc18c2de3c80ce7d26207681e0 to your computer and use it in GitHub Desktop.
Save englishextra/3053a4dc18c2de3c80ce7d26207681e0 to your computer and use it in GitHub Desktop.
/*!
* Escape strings for use as JavaScript string literals
* gist.github.com/englishextra/3053a4dc18c2de3c80ce7d26207681e0
* modified github.com/joliss/js-string-escape
*/
jsStringEscape = function (b) {
return ("" + b).replace(/["'\\\n\r\u2028\u2029]/g, function (a) {
switch (a) {
case '"':
case "'":
case "\\":
return "\\" + a;
case "\n":
return "\\n";
case "\r":
return "\\r";
case "\u2028":
return "\\u2028";
case "\u2029":
return "\\u2029"
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment