admin管理员组文章数量:1406728
How can I split foreign characters, such as Chinese, into separate array values using JavaScript?
split()
seems to work well with English, but not so much with Chinese. See below result of two strings
a) Hello There
b) 你好吗
splitString = text.split(" ");
RESULT: ["hello", "there"]
RESULT: ["你好吗"]
How can I split foreign characters, such as Chinese, into separate array values using JavaScript?
split()
seems to work well with English, but not so much with Chinese. See below result of two strings
a) Hello There
b) 你好吗
splitString = text.split(" ");
RESULT: ["hello", "there"]
RESULT: ["你好吗"]
Share
Improve this question
asked Oct 6, 2015 at 2:58
user3871user3871
12.7k36 gold badges140 silver badges283 bronze badges
2
-
1
You seem to be confused about the nature of "words" in Chinese. Chinese can be considered to have a concept of "words", but it is not necessarily well-defined. You are looking for the idea of "segmentation", but segmentation in Chinese (and other languages without spaces, including Thai, Korean, and Japanese) is a quite plex linguistic task, which as another menter mentioned, is implemented in libraries. On the other hand, if you merely want to split by character, then
String#split
does exactly what you want in most cases. – user663031 Commented Oct 6, 2015 at 4:32 - Note that all answers in this question use character split, which is the same as How do you get a string to a character array in JavaScript?. – user202729 Commented Aug 30, 2018 at 6:22
2 Answers
Reset to default 6There is no way to do that reliably using built-in ES5 facilities without using any 3rd party libraries.
The correct way using vanilla JS is to use ES2015 spread operator:
let splitString = [...text];
Examples of strings which would cause the
本文标签:
javascriptSplit Chinese CharactersStack Overflow
版权声明:本文标题:javascript - Split Chinese Characters - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1745036193a2638800.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
split
-based solutions to fail:
发表评论