Created
August 5, 2011 03:23
-
-
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
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
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