Skip to content

Instantly share code, notes, and snippets.

@myaumyau
Last active December 18, 2015 20:49
Show Gist options
  • Save myaumyau/5843345 to your computer and use it in GitHub Desktop.
Save myaumyau/5843345 to your computer and use it in GitHub Desktop.
[js]文字列系
/**
* Unicode文字列をunescape
*/
var unescapeUnicode = function(str) {
return str.replace(/\\u([a-fA-F0-9]{4})/g, function(matchedString, group1) {
return String.fromCharCode(parseInt(group1, 16));
});
};
unescapeUnicode('\u005baA\u0020\u3042\u4e9c\u005d\u005c'); // [aA あ亜]\
/**
* 文字列をUnicode文字列に変換
*/
var toUnicodeString = function(str) {
return str.replace(/\W/g, function(s) {
return '\\u' +('0000'+ s.charCodeAt(0).toString(16)).slice(-4);
});
};
toUnicodeString('[aA あ亜]\\'); // \u005baA\u0020\u3042\u4e9c\u005d\u005c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment