UNPKG

3.56 kBMarkdownView Raw
1# emoji-regex [![Build status](https://github.com/mathiasbynens/emoji-regex/actions/workflows/main.yml/badge.svg)](https://github.com/mathiasbynens/emoji-regex/actions/workflows/main.yml) [![emoji-regex on npm](https://img.shields.io/npm/v/emoji-regex)](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
7Via [npm](https://www.npmjs.com/):
8
9```bash
10npm install emoji-regex
11```
12
13In [Node.js](https://nodejs.org/):
14
15```js
16const 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
22const 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
29const regex = emojiRegex();
30for (const match of text.matchAll(regex)) {
31 const emoji = match[0];
32 console.log(`Matched sequence ${ emoji } β€” code points: ${ [...emoji].length }`);
33}
34```
35
36Console output:
37
38```
39Matched sequence ⌚ β€” code points: 1
40Matched sequence ⌚ β€” code points: 1
41Matched sequence ↔️ β€” code points: 2
42Matched sequence ↔️ β€” code points: 2
43Matched sequence πŸ‘© β€” code points: 1
44Matched sequence πŸ‘© β€” code points: 1
45Matched sequence πŸ‘©πŸΏ β€” code points: 2
46Matched sequence πŸ‘©πŸΏ β€” code points: 2
47```
48
49## For maintainers
50
51### How to update emoji-regex after new Unicode Standard releases
52
531. [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
551. Bump the _emoji-test-regex-pattern_ dependency to the latest version.
56
571. 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
791. 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
891. 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| [![twitter/mathias](https://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](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.