[英]How can a javascript string with special chars(🕵️,👏) be split into individual characters?
I'm working with a component that splits text into characters.我正在使用一个将文本拆分为字符的组件。 My expectation when splitting was that I will get 🕵️ and characters like it as a single character.拆分时我的期望是我将获得🕵️和喜欢它的角色作为单个角色。
Lets say we have the following text: "Hello 🕵️ world 👏".假设我们有以下文本:“你好🕵️世界👏”。 If I split the text with:如果我将文本拆分为:
"Hello 🕵️ world 👏".split('')
// result: "H", "e", "l", "l", "o", " ", "\ud83d", "\udd75", "️", "", …
"🕵️".split('')
// result: Array [ "\ud83d", "\udd75", "️", "" ]
How can I split the string in such a way that these type of symbols are treated as one entity?如何以将这些类型的符号视为一个实体的方式拆分字符串?
Try using the spread operator instead of .split()
:尝试使用扩展运算符而不是.split()
:
[..."Hello 🕵️ world 👏"]
As far as I know, the reason spread
works and split
doesn't is that String.split
is separating zero-width joiners and spread
is not.据我所知, spread
有效而split
无效的原因是String.split
正在分隔零宽度连接器,而spread
不是。
"🕵️".split().length // 4
[..."🕵️"].length // 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.