Last active
December 18, 2015 20:49
-
-
Save myaumyau/5843345 to your computer and use it in GitHub Desktop.
[js]文字列系
This file contains hidden or 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
/** | |
* 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