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
Add a ment  | 

2 Answers 2

Reset to default 6

There 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 split-based solutions to fail:

本文标签: javascriptSplit Chinese CharactersStack Overflow