Created
September 26, 2011 19:50
-
-
Save mathiasbynens/1243213 to your computer and use it in GitHub Desktop.
Escape all characters in a string using both Unicode and hexadecimal escape sequences
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
// Ever needed to escape '\n' as '\\n'? This function does that for any character, | |
// using hex and/or Unicode escape sequences (whichever are shortest). | |
// Demo: http://mothereff.in/js-escapes | |
function unicodeEscape(str) { | |
return str.replace(/[\s\S]/g, function(character) { | |
var escape = character.charCodeAt().toString(16), | |
longhand = escape.length > 2; | |
return '\\' + (longhand ? 'u' : 'x') + ('0000' + escape).slice(longhand ? -4 : -2); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@mathiasbynens It looks great! I did try to use it but unfortunately I'm not up to date with all the browserify/bundling stuff and just need a vanilla JS script (e.g. no use of
Buffer
) to include in a module import and wasn't able to work out how to do that withjsesc
(though I admit I only poked around for a few minutes before deciding to write the function above). Also, out of pure curiosity I'd be interested in cases where the above function fails - I couldn't find any failing cases in my tests.