1

I am working on a React Native chat app where I need two regex's one that detects if a string contains an emoji and another that detects if a string consists of emojis only for styling a large list of messages

After some research, the fastest and most complete regex I found for detecting if a string has an emoji (stops after the first match) which works perfectly:

 /(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]|\ud83c[\udffb-\udfff])?(?:\u200d(?:[^\ud800-\udfff]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]|\ud83c[\udffb-\udfff])?)*/

I barley understand how its working so I refactored this to detect if a string contains only emojis --5 being the max number of emojis

/^((?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]|\ud83c[\udffb-\udfff])?(?:\u200d(?:[^\ud800-\udfff]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]|\ud83c[\udffb-\udfff])?)*){0,5}$/

by just grouping the whole regex and adding ^ at the start and {0,5}$ at the end

is there a better way to do the second regex?

mfy316
  • 129
  • 10
  • What do you consider to be an emoji? – ASDFGerte Jan 07 '20 at 02:41
  • native iOS & android emojis – mfy316 Jan 07 '20 at 02:46
  • Acc. to [Emoji Keyboard/Display Test Data for UTR #51 (Version: 12.1)](https://unicode.org/Public/emoji/12.1/emoji-test.txt), there are 4022 emojis, your pattern only matches 1695 of them. – Wiktor Stribiżew Jan 07 '20 at 08:54
  • *stops after the first match* - you did not specify the `g` flag. If you need to match any 0 or more emojis, you should not wrap the `(...)*` with another group, just add `^` at the start and `$` at the end (right after `*`). – Wiktor Stribiżew Jan 07 '20 at 09:04

0 Answers0