Split Chinese Characters

后端 未结 2 1554
一生所求
一生所求 2021-01-15 13:09

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 m

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-15 13:46

    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:

提交回复
热议问题