Reputation: 1805
I have a string in JavaScript like so:
var string = "🌲🥒 Hello 👋 world!😂"; // Length of emoji at end: 2 characters ("😂".length = 2)
Or:
var string = "🛁 Hello world! 👨👩👧"; // Length of emoji at end: 8 characters ("👨👩👧".length = 8)
Or a string without an emoji:
var string = "🥒 Hello world!"; // No emoji at end
I need to find the out following:
I came as far as detecting two-character emojis with the following code:
var emoji = new RegExp('\\p{Extended_Pictographic}', 'u');
var isEmoji = emoji.test(string.slice(-2)); // true for the first string
However, this obviously does not detect the length (e.g. 👨👩👧 = 8). If anyone has experience with emojis in JavaScript and could point me in the right direction it would be very much appreciated.
Upvotes: 0
Views: 273
Reputation: 1021
The rule is that every emoji that consists of several unicode characters is simply a combination of several emojis, so if you divide the entire string into an array by using the spread operator, then if the last element was an emoji, no matter what its length... then now the last element in the array must also be Emoji
var string1 = "🛁 Hello world! 👨👩👧";
var string2 = "🛁 Hello world! 👨👩👧!";
var string3 = "🌲🥒 Hello 👋 world!😂";
var string4 = "🌲🥒 Hello 👋 world!😂!";
var emoji = new RegExp('\\p{Extended_Pictographic}', 'u');
var isEmoji1 = emoji.test([...string1].at(-1));
var isEmoji2 = emoji.test([...string2].at(-1));
var isEmoji3 = emoji.test([...string3].at(-1));
var isEmoji4 = emoji.test([...string4].at(-1));
console.log(isEmoji1)
console.log(isEmoji2)
console.log(isEmoji3)
console.log(isEmoji4)
Upvotes: 1