Skip to content

Instantly share code, notes, and snippets.

@gom
Created August 5, 2011 03:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gom/1126857 to your computer and use it in GitHub Desktop.
Save gom/1126857 to your computer and use it in GitHub Desktop.
Unicode escape / unescape on javascript via http://d.hatena.ne.jp/sawat/20070309/1173459459
var unicode = {
escape: function(s) {
return s.replace(/^[-~]|\\/g, function(m) {
var code = m.charCodeAt(0);
return '\\u' + ((code < 0x10) ? '000' : ((code < 0x100) ? '00' : ((code < 0x1000) ? '0' : ''))) + code.toString(16);
});
},
unescape : function (s) {
return s.replace(/\\u([a-fA-F0-9]{4})/g, function(matched, g1) {
return String.fromCharCode(parseInt(g1, 16))
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment