Last active
August 29, 2015 14:17
-
-
Save YusukeHirao/67da6a51b676b6add822 to your computer and use it in GitHub Desktop.
JavaScriptでのサロゲートペア文字列のメモ ref: http://qiita.com/YusukeHirao/items/2f0fb8d5bbb981101be0
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
// シングルクォートとダブルクォートの差はない | |
"\u3042"; // => "あ" | |
'\u3042'; // => "あ" | |
// 正規表現リテラルでも表現可能 | |
/\u3042/.test('あ'); // => true |
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
// 10進数の参照 (戻り値型: number) | |
'あ'.charCodeAt(0); // => 12354 | |
// 16進数の参照 (戻り値型: string) | |
'あ'.charCodeAt(0).toString(16); // => "3042" |
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
// シンプルに書くとこう | |
// ※String.fromCodePointに似せるなら可変長引数に対応する必要あり | |
function stringFromCodePoint (codeNum) { | |
var cp = codeNum - 0x10000; | |
var high = 0xD800 | (cp >> 10); | |
var low = 0xDC00 | (cp & 0x3FF); | |
return String.fromCharCode(high, low); | |
} | |
stringFromCodePoint(0x29E3D); // => "𩸽" |
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
function stringToArray (str) { | |
return str.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]|[^\uD800-\uDFFF]/g) || []; | |
} | |
stringToArray('𩸽のひらき'); // => ["𩸽", "の", "ひ", "ら", "き"] |
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
stringToArray('𩸽のひらき').length; // => 5 |
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
stringToArray('𩸽のひらき').length; // => 5 |
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
stringToArray('𩸽のひらき')[0]; // => "𩸽" | |
stringToArray('𩸽のひらき')[1]; // => "の" |
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
// 10進数から参照 | |
String.fromCharCode(12354); // => "あ" | |
// 16進数から参照 数値の16進数リテラルを利用すれば可能 | |
String.fromCharCode(0x3042); // => "あ" |
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
'\uD867\uDE3D'; // => "𩸽" |
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
'\u29E3D'; // => "⧣D" |
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
'𩸽'.length; // => 2 |
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
console.log('𩸽'.split('')); // => ["�", "�"] |
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
'𩸽のひらき'.charAt(1); // => "�" | |
'𩸽のひらき'[2]; // => "の" | |
'𩸽のひらき'.slice(1, 4); // => "�のひ" ※表示媒体によっては下位サロゲート以降の文字が表示されない |
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
String.fromCodePoint(0x29E3D); // => "𩸽" |
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
"\u{29E3D}"; // => "𩸽" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment