Skip to main content

Regex patterns

Matching emoji characters within a string can be difficult, as multiple codepoints, surrogate pairs, variation selectors, zero width joiners, so on and so forth, must be taken into account. To make this whole process easier, pre-built regex patterns are available in the emojibase-regex package.

yarn add emojibase-regex

Usage​

As stated, there are 7 regex patterns. One for matching emoji presentation characters, one for matching text presentation characters, one for matching both types of characters, and the last for matching shortcodes or emoticons.

  • emojibase-regex - Matches every emoji, in emoji or text presentation, including forms that are missing an FE0F.
  • emojibase-regex/emoji - Matches emoji in emoji presentation: emoji that default to emoji presentation (πŸ‘), emoji followed by FE0F (©️), and every fully-qualified sequence.
  • emojibase-regex/emoji-loose - Like the above but also includes forms that are missing an FE0F, like a bare Β© or πŸ•΅β€β™‚.
  • emojibase-regex/text - Matches emoji in text presentation: emoji followed by FE0E (πŸ‘οΈŽ), and emoji that default to text presentation (Β©).
  • emojibase-regex/text-loose - Like the above but also includes emoji that default to emoji presentation and have a text presentation form, like a bare πŸ‘.
  • emojibase-regex/emoticon - Matches supported emoticons and their permutations.
  • emojibase-regex/shortcode - Matches supported shortcodes.
  • emojibase-regex/shortcode-native - Matches supported shortcodes in their native language (cldr-native).

Each of these imports return a RegExp instance with no flags defined.

The emoji and text patterns never match a character that a selector or skin tone puts in the other presentation, so the text patterns do not match the ⚠ in ⚠️. The loose text pattern can still match the last character of a ZWJ sequence, like the πŸŽ“ in πŸ§‘β€πŸŽ“.

import EMOJI_REGEX from 'emojibase-regex';
import EMOTICON_REGEX from 'emojibase-regex/emoticon';
import SHORTCODE_REGEX from 'emojibase-regex/shortcode';
import SHORTCODE_NATIVE_REGEX from 'emojibase-regex/shortcode-native';

`πŸ™‚`.match(EMOJI_REGEX);
':)'.match(EMOTICON_REGEX);
':pleased:'.match(SHORTCODE_REGEX);
':гвинСя:'.match(SHORTCODE_NATIVE_REGEX);

The u (unicode) and g (global) flags are not defined on these patterns.

The emoticon regex does not include word boundaries.

Unicode codepoint support​

By default, regex patterns are generated using hexadecimal Unicode ranges. If desired, ES2015+ Unicode codepoint aware regex patterns can be used, which can be found in the codepoint directory.

import CODEPOINT_EMOJI_REGEX from 'emojibase-regex/codepoint';

The u (unicode) flag is required (defined by default) when using these patterns.

Codepoint regex patterns are only supported in Node.js and modern browsers.

Unicode property support​

Patterns built from Unicode property escapes, like \p{Emoji}, can be found in the property directory. They are much smaller than the other patterns, and follow the definitions in UTS #51 rather than a list of emoji:

  • emojibase-regex/property - Possible emoji, after the grammar in UTS #51.
  • emojibase-regex/property/emoji - Fully-qualified emoji, in emoji presentation.
  • emojibase-regex/property/text - Emoji in text presentation.
import PROPERTY_EMOJI_REGEX from 'emojibase-regex/property';

The u (unicode) flag is defined on these patterns.

They also match well-formed sequences that are not recommended for general use, and they rely on the JavaScript engine's own Unicode data, so emoji newer than the engine do not match.

Filesizes​

FileSizeGzipped
shortcode.js34 B54 B
property/text.js387 B283 B
emoticon.js463 B244 B
property/index.js525 B363 B
property/emoji.js621 B366 B
shortcode-native.js692 B425 B
text.js2.58 kB773 B
codepoint/text.js3.09 kB806 B
text-loose.js3.11 kB672 B
codepoint/text-loose.js3.8 kB708 B
emoji.js12.85 kB2.36 kB
codepoint/emoji.js12.96 kB2.41 kB
codepoint/emoji-loose.js16.61 kB2.51 kB
codepoint/index.js16.61 kB2.5 kB
emoji-loose.js16.67 kB2.47 kB
index.js16.67 kB2.48 kB