You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.6 KiB

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