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
- NPM
yarn add emojibase-regex
npm install 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 anFE0F.emojibase-regex/emoji- Matches emoji in emoji presentation: emoji that default to emoji presentation (π), emoji followed byFE0F(Β©οΈ), and every fully-qualified sequence.emojibase-regex/emoji-loose- Like the above but also includes forms that are missing anFE0F, like a bare Β© or π΅ββ.emojibase-regex/text- Matches emoji in text presentation: emoji followed byFE0E(ποΈ), 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) andg(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β
| File | Size | Gzipped |
|---|---|---|
| shortcode.js | 34 B | 54 B |
| property/text.js | 387 B | 283 B |
| emoticon.js | 463 B | 244 B |
| property/index.js | 525 B | 363 B |
| property/emoji.js | 621 B | 366 B |
| shortcode-native.js | 692 B | 425 B |
| text.js | 2.58 kB | 773 B |
| codepoint/text.js | 3.09 kB | 806 B |
| text-loose.js | 3.11 kB | 672 B |
| codepoint/text-loose.js | 3.8 kB | 708 B |
| emoji.js | 12.85 kB | 2.36 kB |
| codepoint/emoji.js | 12.96 kB | 2.41 kB |
| codepoint/emoji-loose.js | 16.61 kB | 2.51 kB |
| codepoint/index.js | 16.61 kB | 2.5 kB |
| emoji-loose.js | 16.67 kB | 2.47 kB |
| index.js | 16.67 kB | 2.48 kB |