find emojis in a string using regex in javascript

To find emojis in a string using regex in JavaScript, you can use the Unicode code points of emojis. An emoji can be represented by one or more Unicode code points, so you need to identify the code points for the emojis you want to match.

Here's an example regex pattern that matches common emojis:

index.tsx
const emojiRegex = /[\u{1F600}-\u{1F64F}|\u{2702}-\u{27B0}|\u{1F680}-\u{1F6FF}|\u{1F170}-\u{1F251}|\u{1F300}-\u{1F5FF}|\u{1F900}-\u{1F9FF}]/gu;
144 chars
2 lines

This regex uses the Unicode code point ranges for the following emoji categories:

  • Emoticons (1F600 - 1F64F)
  • Dingbats (2702 - 27B0)
  • Transport & Map Symbols (1F680 - 1F6FF)
  • Enclosed Characters (1F170 - 1F251)
  • Miscellaneous Symbols and Pictographs (1F300 - 1F5FF)
  • Supplemental Symbols and Pictographs (1F900 - 1F9FF)

You can then use the .match() method of a string to find all matches of emojis in the string:

index.tsx
const string = "👋 Hello, 🌎!";
const emojis = string.match(emojiRegex);
console.log(emojis); // ["👋", "🌎"]
110 chars
4 lines

Note that not all emojis may be included in this regex pattern, as new emojis are added to Unicode with each new version. You may need to update the regex pattern to include the code points for any new emojis you want to match.

gistlibby LogSnag