1 | # emoji-regex [![Build status](https://travis-ci.org/mathiasbynens/emoji-regex.svg?branch=master)](https://travis-ci.org/mathiasbynens/emoji-regex)
|
2 |
|
3 | _emoji-regex_ offers a regular expression to match all emoji symbols (including textual representations of emoji) as per the Unicode Standard.
|
4 |
|
5 | This repository contains a script that generates this regular expression based on [the data from Unicode v12](https://github.com/mathiasbynens/unicode-12.0.0). Because of this, the regular expression can easily be updated whenever new emoji are added to the Unicode standard.
|
6 |
|
7 | ## Installation
|
8 |
|
9 | Via [npm](https://www.npmjs.com/):
|
10 |
|
11 | ```bash
|
12 | npm install emoji-regex
|
13 | ```
|
14 |
|
15 | In [Node.js](https://nodejs.org/):
|
16 |
|
17 | ```js
|
18 | const emojiRegex = require('emoji-regex');
|
19 | // Note: because the regular expression has the global flag set, this module
|
20 | // exports a function that returns the regex rather than exporting the regular
|
21 | // expression itself, to make it impossible to (accidentally) mutate the
|
22 | // original regular expression.
|
23 |
|
24 | const text = `
|
25 | \u{231A}: β default emoji presentation character (Emoji_Presentation)
|
26 | \u{2194}\u{FE0F}: βοΈ default text presentation character rendered as emoji
|
27 | \u{1F469}: π© emoji modifier base (Emoji_Modifier_Base)
|
28 | \u{1F469}\u{1F3FF}: π©πΏ emoji modifier base followed by a modifier
|
29 | `;
|
30 |
|
31 | const regex = emojiRegex();
|
32 | let match;
|
33 | while (match = regex.exec(text)) {
|
34 | const emoji = match[0];
|
35 | console.log(`Matched sequence ${ emoji } β code points: ${ [...emoji].length }`);
|
36 | }
|
37 | ```
|
38 |
|
39 | Console output:
|
40 |
|
41 | ```
|
42 | Matched sequence β β code points: 1
|
43 | Matched sequence β β code points: 1
|
44 | Matched sequence βοΈ β code points: 2
|
45 | Matched sequence βοΈ β code points: 2
|
46 | Matched sequence π© β code points: 1
|
47 | Matched sequence π© β code points: 1
|
48 | Matched sequence π©πΏ β code points: 2
|
49 | Matched sequence π©πΏ β code points: 2
|
50 | ```
|
51 |
|
52 | To match emoji in their textual representation as well (i.e. emoji that are not `Emoji_Presentation` symbols and that arenβt forced to render as emoji by a variation selector), `require` the other regex:
|
53 |
|
54 | ```js
|
55 | const emojiRegex = require('emoji-regex/text.js');
|
56 | ```
|
57 |
|
58 | Additionally, in environments which support ES2015 Unicode escapes, you may `require` ES2015-style versions of the regexes:
|
59 |
|
60 | ```js
|
61 | const emojiRegex = require('emoji-regex/es2015/index.js');
|
62 | const emojiRegexText = require('emoji-regex/es2015/text.js');
|
63 | ```
|
64 |
|
65 | ## Author
|
66 |
|
67 | | [![twitter/mathias](https://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](https://twitter.com/mathias "Follow @mathias on Twitter") |
|
68 | |---|
|
69 | | [Mathias Bynens](https://mathiasbynens.be/) |
|
70 |
|
71 | ## License
|
72 |
|
73 | _emoji-regex_ is available under the [MIT](https://mths.be/mit) license.
|