1 | # emoji-regex [](https://github.com/mathiasbynens/emoji-regex/actions/workflows/main.yml) [](https://www.npmjs.com/package/emoji-regex)
|
2 |
|
3 | _emoji-regex_ offers a regular expression to match all emoji symbols and sequences (including textual representations of emoji) as per the Unicode Standard. Itβs based on [_emoji-test-regex-pattern_](https://github.com/mathiasbynens/emoji-test-regex-pattern), which generates (at build time) the regular expression pattern based on the Unicode Standard. As a result, _emoji-regex_ can easily be updated whenever new emoji are added to Unicode.
|
4 |
|
5 | ## Installation
|
6 |
|
7 | Via [npm](https://www.npmjs.com/):
|
8 |
|
9 | ```bash
|
10 | npm install emoji-regex
|
11 | ```
|
12 |
|
13 | In [Node.js](https://nodejs.org/):
|
14 |
|
15 | ```js
|
16 | const emojiRegex = require('emoji-regex');
|
17 | // Note: because the regular expression has the global flag set, this module
|
18 | // exports a function that returns the regex rather than exporting the regular
|
19 | // expression itself, to make it impossible to (accidentally) mutate the
|
20 | // original regular expression.
|
21 |
|
22 | const text = `
|
23 | \u{231A}: β default emoji presentation character (Emoji_Presentation)
|
24 | \u{2194}\u{FE0F}: βοΈ default text presentation character rendered as emoji
|
25 | \u{1F469}: π© emoji modifier base (Emoji_Modifier_Base)
|
26 | \u{1F469}\u{1F3FF}: π©πΏ emoji modifier base followed by a modifier
|
27 | `;
|
28 |
|
29 | const regex = emojiRegex();
|
30 | for (const match of text.matchAll(regex)) {
|
31 | const emoji = match[0];
|
32 | console.log(`Matched sequence ${ emoji } β code points: ${ [...emoji].length }`);
|
33 | }
|
34 | ```
|
35 |
|
36 | Console output:
|
37 |
|
38 | ```
|
39 | Matched sequence β β code points: 1
|
40 | Matched sequence β β code points: 1
|
41 | Matched sequence βοΈ β code points: 2
|
42 | Matched sequence βοΈ β code points: 2
|
43 | Matched sequence π© β code points: 1
|
44 | Matched sequence π© β code points: 1
|
45 | Matched sequence π©πΏ β code points: 2
|
46 | Matched sequence π©πΏ β code points: 2
|
47 | ```
|
48 |
|
49 | ## For maintainers
|
50 |
|
51 | ### How to update emoji-regex after new Unicode Standard releases
|
52 |
|
53 | 1. [Update _emoji-test-regex-pattern_ as described in its repository](https://github.com/mathiasbynens/emoji-test-regex-pattern#how-to-update-emoji-test-regex-pattern-after-new-uts51-releases).
|
54 |
|
55 | 1. Bump the _emoji-test-regex-pattern_ dependency to the latest version.
|
56 |
|
57 | 1. Update the Unicode data dependency in `package.json` by running the following commands:
|
58 |
|
59 | ```sh
|
60 | # Example: updating from Unicode v13 to Unicode v14.
|
61 | npm uninstall @unicode/unicode-13.0.0
|
62 | npm install @unicode/unicode-14.0.0 --save-dev
|
63 | ````
|
64 |
|
65 | 1. Generate the new output:
|
66 |
|
67 | ```sh
|
68 | npm run build
|
69 | ```
|
70 |
|
71 | 1. Verify that tests still pass:
|
72 |
|
73 | ```sh
|
74 | npm test
|
75 | ```
|
76 |
|
77 | ### How to publish a new release
|
78 |
|
79 | 1. On the `main` branch, bump the emoji-regex version number in `package.json`:
|
80 |
|
81 | ```sh
|
82 | npm version patch -m 'Release v%s'
|
83 | ```
|
84 |
|
85 | Instead of `patch`, use `minor` or `major` [as needed](https://semver.org/).
|
86 |
|
87 | Note that this produces a Git commit + tag.
|
88 |
|
89 | 1. Push the release commit and tag:
|
90 |
|
91 | ```sh
|
92 | git push && git push --tags
|
93 | ```
|
94 |
|
95 | Our CI then automatically publishes the new release to npm.
|
96 |
|
97 | ## Author
|
98 |
|
99 | | [](https://twitter.com/mathias "Follow @mathias on Twitter") |
|
100 | |---|
|
101 | | [Mathias Bynens](https://mathiasbynens.be/) |
|
102 |
|
103 | ## License
|
104 |
|
105 | _emoji-regex_ is available under the [MIT](https://mths.be/mit) license.
|