How can I split a string containing emoji into an array?

后端 未结 6 560
慢半拍i
慢半拍i 2020-12-01 08:12

(You\'ll need Firefox or Safari to see the emoji in the code.)

I want to take a string of emoji and do something with the individual characters.

In JavaScript

6条回答
  •  天涯浪人
    2020-12-01 08:38

    It can be done using the u flag of a regular expression. The regular expression is:

    /.*?/u
    

    This is broken every time there are there are at least minimally zero or more characters that may or may not be emojis, but cannot be spaces or new lines break.

    • There are at least minimally zero or more: ? (split in zero chars)
    • Zero or more: *
    • Cannot be spaces or new line break: .
    • May or may not be emojis: /u

    By using the question mark ? I am forcing to cut exactly every zero chars, otherwise /.*/u it cuts by all characters until I find a space or newline break.

    var string = "

提交回复
热议问题