简体   繁体   中英

Split Chinese Characters

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: ["你好吗"]

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: 𠀁

而不是拆分一个空格字符(中文字符串中没有空格),请尝试拆分一个空字符串“”,该字符串应将每个字符拆分为自己的元素。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM