我正在尝试对一些嵌入了表情符号的文本执行一些字符串方法。
但是,这是我见过的一件非常奇怪的事情:
"🌑".length == 2
我只是想知道它是如何决定对我来说显示为 1 个字符的,实际上是 2 个。
最佳答案
In Javascript, a string is a sequence of 16-bit code points. Since emoji > are encoded above the BMP, it means that they are represented by a pair > of code points, also known as a surrogate pair. So for instance, 0x1F600, which is 😀, is represented by:
block 引用>"\uD83D\uDE00"
如果您愿意进一步了解这一点,可以阅读这篇文章:Emojis in Javascript - Parsing emoji in Javascript is… not easy.
关于Javascript + Emoji 奇怪的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21802329/