| [9af201e] | 1 | # emoji-regex [](https://travis-ci.org/mathiasbynens/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.
|
|---|
| 4 |
|
|---|
| 5 | This repository contains a script that generates this regular expression based on [Unicode data](https://github.com/node-unicode/node-unicode-data). 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/RGI_Emoji.js');
|
|---|
| 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 | ## Regular expression flavors
|
|---|
| 53 |
|
|---|
| 54 | The package comes with three distinct regular expressions:
|
|---|
| 55 |
|
|---|
| 56 | ```js
|
|---|
| 57 | // This is the recommended regular expression to use. It matches all
|
|---|
| 58 | // emoji recommended for general interchange, as defined via the
|
|---|
| 59 | // `RGI_Emoji` property in the Unicode Standard.
|
|---|
| 60 | // https://unicode.org/reports/tr51/#def_rgi_set
|
|---|
| 61 | // When in doubt, use this!
|
|---|
| 62 | const emojiRegexRGI = require('emoji-regex/RGI_Emoji.js');
|
|---|
| 63 |
|
|---|
| 64 | // This is the old regular expression, prior to `RGI_Emoji` being
|
|---|
| 65 | // standardized. In addition to all `RGI_Emoji` sequences, it matches
|
|---|
| 66 | // some emoji you probably don’t want to match (such as emoji component
|
|---|
| 67 | // symbols that are not meant to be used separately).
|
|---|
| 68 | const emojiRegex = require('emoji-regex/index.js');
|
|---|
| 69 |
|
|---|
| 70 | // This regular expression matches even more emoji than the previous
|
|---|
| 71 | // one, including emoji that render as text instead of icons (i.e.
|
|---|
| 72 | // emoji that are not `Emoji_Presentation` symbols and that aren’t
|
|---|
| 73 | // forced to render as emoji by a variation selector).
|
|---|
| 74 | const emojiRegexText = require('emoji-regex/text.js');
|
|---|
| 75 | ```
|
|---|
| 76 |
|
|---|
| 77 | Additionally, in environments which support ES2015 Unicode escapes, you may `require` ES2015-style versions of the regexes:
|
|---|
| 78 |
|
|---|
| 79 | ```js
|
|---|
| 80 | const emojiRegexRGI = require('emoji-regex/es2015/RGI_Emoji.js');
|
|---|
| 81 | const emojiRegex = require('emoji-regex/es2015/index.js');
|
|---|
| 82 | const emojiRegexText = require('emoji-regex/es2015/text.js');
|
|---|
| 83 | ```
|
|---|
| 84 |
|
|---|
| 85 | ## For maintainers
|
|---|
| 86 |
|
|---|
| 87 | ### How to update emoji-regex after new Unicode Standard releases
|
|---|
| 88 |
|
|---|
| 89 | 1. Update the Unicode data dependency in `package.json` by running the following commands:
|
|---|
| 90 |
|
|---|
| 91 | ```sh
|
|---|
| 92 | # Example: updating from Unicode v12 to Unicode v13.
|
|---|
| 93 | npm uninstall @unicode/unicode-12.0.0
|
|---|
| 94 | npm install @unicode/unicode-13.0.0 --save-dev
|
|---|
| 95 | ````
|
|---|
| 96 |
|
|---|
| 97 | 1. Generate the new output:
|
|---|
| 98 |
|
|---|
| 99 | ```sh
|
|---|
| 100 | npm run build
|
|---|
| 101 | ```
|
|---|
| 102 |
|
|---|
| 103 | 1. Verify that tests still pass:
|
|---|
| 104 |
|
|---|
| 105 | ```sh
|
|---|
| 106 | npm test
|
|---|
| 107 | ```
|
|---|
| 108 |
|
|---|
| 109 | 1. Send a pull request with the changes, and get it reviewed & merged.
|
|---|
| 110 |
|
|---|
| 111 | 1. On the `main` branch, bump the emoji-regex version number in `package.json`:
|
|---|
| 112 |
|
|---|
| 113 | ```sh
|
|---|
| 114 | npm version patch -m 'Release v%s'
|
|---|
| 115 | ```
|
|---|
| 116 |
|
|---|
| 117 | Instead of `patch`, use `minor` or `major` [as needed](https://semver.org/).
|
|---|
| 118 |
|
|---|
| 119 | Note that this produces a Git commit + tag.
|
|---|
| 120 |
|
|---|
| 121 | 1. Push the release commit and tag:
|
|---|
| 122 |
|
|---|
| 123 | ```sh
|
|---|
| 124 | git push
|
|---|
| 125 | ```
|
|---|
| 126 |
|
|---|
| 127 | Our CI then automatically publishes the new release to npm.
|
|---|
| 128 |
|
|---|
| 129 | ## Author
|
|---|
| 130 |
|
|---|
| 131 | | [](https://twitter.com/mathias "Follow @mathias on Twitter") |
|
|---|
| 132 | |---|
|
|---|
| 133 | | [Mathias Bynens](https://mathiasbynens.be/) |
|
|---|
| 134 |
|
|---|
| 135 | ## License
|
|---|
| 136 |
|
|---|
| 137 | _emoji-regex_ is available under the [MIT](https://mths.be/mit) license.
|
|---|