Split JavaScript string into array of codepoints? (taking into account “surrogate pairs” but not “grapheme clusters”)

后端 未结 4 1140
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-10 12:19

Splitting a JavaScript string into \"characters\" can be done trivially but there are problems if you care about Unicode (and you should care about Unicode).

JavaScr

4条回答
  •  有刺的猬
    2020-12-10 12:36

    Along the lines of @John Frazer's answer, one can use this even succincter form of string iteration:

    const chars = [...text]
    

    e.g., with:

    const text = 'A\uD835\uDC68B\uD835\uDC69C\uD835\uDC6A'
    const chars = [...text] // ["A", "

提交回复
热议问题