How to count emoji in a text or string

后端 未结 4 911
渐次进展
渐次进展 2021-01-18 07:49

How to count EMOJI in a string or text line and display the output, how many emoji are in the text line or string?

For example:

相关标签:
4条回答
  • 2021-01-18 08:31

    One option would be to compare the length of the input string against the length of the same string with all Emoji characters removed:

    function fancyCount(str){
        return Array.from(str.split(/[\ufe00-\ufe0f]/).join("")).length;
    }
    
    var input = "Hello                                                                     
    0 讨论(0)
  • 2021-01-18 08:31

    The accepted answer does not work in every case, such as the ❤️ emoji which is counted as 0 and some emojis are counted as more than '1' like the

    0 讨论(0)
  • 2021-01-18 08:47

    You can try this lib https://github.com/mathiasbynens/emoji-regex It provides emojiRegex. So you can match emojis in your test like this:

    const text = "Hello                                                                     
    0 讨论(0)
  • 2021-01-18 08:54

    You can check the char code for the emojis and count them that way.

    0 讨论(0)
提交回复
热议问题