JS: String.prototype.codePointAt (char to id)
(new in ECMAScript 2015)
str.codePointAt(index)-
Return a integer that's the Code Point of character at position index of str.
console.log("ABC".codePointAt(0)); // 65 // character: A (codepoint 65, #x41) console.log("ABC".codePointAt(1)); // 66 // character: B (codepoint 66, #x42) /* 🦋 BUTTERFLY ID 129419 HEXD 1F98B UTF16 D83E DD8B */ // get the codepoint console.log("🦋".codePointAt(0)); // 129419 // get the codepoint in hexadecimal console.log("🦋".codePointAt(0).toString(16)); // 1f98b // s------------------------------ // warning. // if index is at the second part of code unit, it returns the second part of Surrogate Pair console.log("🦋".codePointAt(1).toString(16)); // dd8b 🛑 warning: string methods do not work the way you think if it contains characters outside of Unicode Basic Multilingual Plane (e.g. emoji 🦋.). See JS: String Index Code Unit
// codePointAt does not work well if you have emoji or rare unicode in string // we want to get code point of b // The character b codepoint is 98. console.log("🦋b".codePointAt(1)); // 56715 // wrong. // 56715 in hexadecimal it is console.log("🦋b".codePointAt(1).toString(16)); // dd8b /* dd8b is second half of the butterfly in UTF16. */ /* 🦋 BUTTERFLY ID 129419 HEXD 1F98B UTF8 F0 9F A6 8B UTF16 D83E DD8B */
See also: Unicode Search 🔍