Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count emoji in a text or string [closed]

Tags:

javascript

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: "Hello 😀😁😂🤣😃😄😅 there"

The output should be: 7

like image 823
Joseph Miller Avatar asked Sep 20 '18 08:09

Joseph Miller


People also ask

How do you count emojis?

Emojis or Unicode. Standard emojis count as two characters when writing a message.

How do you check emojis with strings?

Swift 5 Scalars have isEmoji and isEmojiPresentation properties that will help to find emoji in particular String. isEmoji - Boolean value indicating whether the scalar has an emoji presentation, whether or not it is the default.

Are emojis considered text?

Emojis are a modern text in that they have not been a longstanding, traditional form of communication.

How do you get emojis in texts?

During text entry, type Windows logo key + . (period). The emoji keyboard will appear. Select an emoji with the mouse, or keep typing to search through the available emojis for one you like.


2 Answers

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 🍌🍌🍌🍌🍌🍌🍌 there";
var output = input.replace(/([\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2694-\u2697]|\uD83E[\uDD10-\uDD5D])/g, "");
console.log("number of Emoji: " + (fancyCount(input) - fancyCount(output)));

I give massive credit to this helpful blog post, which provided the fancyCount() function. This function can detect that certain Emoji characters actually have a width of 2, while other characters have a width of 1. The issue here is one of encoding. Some Emoji characters may take up two bytes, whereas a basic ASCII character (e.g. A-Z) would only take up one byte.

like image 121
Tim Biegeleisen Avatar answered Oct 03 '22 23:10

Tim Biegeleisen


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 😀😁😂🤣😃😄😅 there"
const regex = emojiRegex();
regex.exec(text)
like image 32
hstn Avatar answered Oct 03 '22 21:10

hstn