How to detect emoji using javascript How to detect emoji using javascript javascript javascript

How to detect emoji using javascript


The answers might work but are terrible because they rely on unicode ranges that are unreadable and somewhat "magic" because it's not always clear where do they come from and why they work, not to mention they're not resilient to new emojis being added to the spec.

Major browsers now support unicode property escape which allows for matching emojis based on their belonging in the Emoji unicode category: \p{Emoji} matches an emoji, \P{Emoji} matches a non-emoji.

Note that officially, 0123456789#* and other characters are emojis too, so the property escape you might want to use is not Emoji but rather Extended_Pictographic which denotes all the characters typically understood as emojis!

Make sure to include the u flag at the end.

console.log(  /\p{Emoji}/u.test('flowers'), // false :)  /\p{Emoji}/u.test('flowers 🌼🌺🌸'), // true :)  /\p{Emoji}/u.test('flowers 123'), // true :( )console.log(  /\p{Extended_Pictographic}/u.test('flowers'), // false :)  /\p{Extended_Pictographic}/u.test('flowers 🌼🌺🌸'), // true :)  /\p{Extended_Pictographic}/u.test('flowers 123'), // false :))