Why EmojiEmoji
A Japanese word (็ตตๆๅญ) meaning 'picture character' โ small graphical symbols used in digital communication to express ideas, emotions, and objects. Regex Is Hard
Writing a regex that correctly matches emoji is surprisingly difficult. A single visible emoji like ๐จโ๐ฉโ๐งโ๐ฆ (family) is composed of 7 UnicodeUnicode
Universal character encoding standard that assigns a unique number to every character across all writing systems and symbol sets, including emoji. code points joined by invisible characters. A regex that matches a single character will match fragments of emoji, or miss them entirely.
The root problems are:
- Variable length: emoji range from 1 code pointCode Point
A unique numerical value assigned to each character in the Unicode standard, written in the format U+XXXX (e.g., U+1F600 for ๐). (๐) to 10+ code points (complex ZWJZero Width Joiner (ZWJ)
An invisible Unicode character (U+200D) used to join multiple emoji into a single composite emoji, such as combining people and objects into profession emoji. sequences) - Surrogate pairs: in UTF-16UTF-16
A variable-width Unicode encoding that uses 2 or 4 bytes per character, used internally by JavaScript, Java, and Windows. environments (JavaScript), each emoji above U+FFFF is two code units - Combining characters: variation selectors, skin tone modifiers, and ZWJ are invisible but part of the emoji
- Evolving standard: new emoji are added each Unicode release, so hardcoded ranges go stale
JavaScript: Using the Unicode Flag
The u flag enables Unicode mode in JavaScript regex, making . match a full code point rather than a single UTF-16 code unitCode Unit
The minimum bit combination used for encoding a character: 8-bit for UTF-8, 16-bit for UTF-16, and 32-bit for UTF-32..
// Without u flag: . matches one code unit (breaks emoji)
/^.$/.test('๐') // false โ emoji is 2 code units
/^.$/u.test('๐') // true โ u flag treats it as one code point
// Match any single emoji code point (basic, not sequences)
const basicEmoji = /\p{Emoji}/u;
basicEmoji.test('Hello ๐') // true
// The v flag (ES2024) adds set operations and is stricter
const emojiV = /[\p{Emoji}--\p{Number}]/v;
Matching Full Emoji Grapheme Clusters
To match complete emoji including ZWJ sequences and skin tones, you need a pattern that handles all the components:
// Comprehensive emoji regex (covers most cases)
const emojiRegex = /\p{Emoji_Modifier_Base}\p{Emoji_Modifier}|\p{Emoji_Presentation}|\p{Emoji}\uFE0F/gu;
// Even better: use the emoji-regex npm package
// import emojiRegex from 'emoji-regex';
// const re = emojiRegex();
// Example usage
const text = 'Hello ๐ World ๐ from ๐จโ๐ป';
const matches = text.match(emojiRegex);
// ['๐', '๐', '๐จโ๐ป'] โ note: ZWJ sequence captured as one match
The emoji-regex Package
For production use, the emoji-regex npm package by Mathias Bynens generates a regex from the Unicode data and handles all edge cases:
import emojiRegex from 'emoji-regex';
const re = emojiRegex();
const str = '๐๐ฝ dancing and ๐ launching';
let match;
while ((match = re.exec(str)) !== null) {
console.log(`Found: ${match[0]} at index ${match.index}`);
}
// Found: ๐๐ฝ at index 0
// Found: ๐ at index 14
Python: The emoji Library and Regex
Python 3 handles code points natively โ '๐' has length 1. But matching emoji sequences still requires care.
Using Unicode Property Escapes with regex
The built-in re module does not support Unicode property escapes. Install the regex module instead:
import regex
# Match emoji with Unicode property escapes
pattern = regex.compile(r'\p{Emoji}', regex.UNICODE)
pattern.findall('Hello ๐ World ๐')
# ['๐', '๐']
# Match grapheme clusters (handles ZWJ sequences)
grapheme_pattern = regex.compile(r'\X', regex.UNICODE)
grapheme_pattern.findall('๐ฉโ๐ป coding')
# ['๐ฉโ๐ป', ' ', 'c', 'o', 'd', 'i', 'n', 'g']
The \X pattern matches a full Unicode grapheme clusterGrapheme Cluster
A user-perceived character that may be composed of multiple Unicode code points displayed as a single visual unit. โ the correct unit for "one visible character."
Using the emoji Library
For higher-level emoji operations, the emoji library is excellent:
import emoji
# Find all emoji in text
text = 'I love ๐ Python and โ coffee'
emoji.emoji_list(text)
# [{'match_start': 7, 'match_end': 8, 'emoji': '๐'},
# {'match_start': 19, 'match_end': 20, 'emoji': 'โ'}]
# Check if string is entirely emoji
emoji.is_emoji('๐') # True
emoji.is_emoji('hello') # False
# Count distinct emoji
emoji.emoji_count('๐๐๐') # 3
emoji.emoji_count('๐๐๐', unique=True) # 1
Matching Specific Emoji Subsets
Flags Only
Country flags are Regional IndicatorRegional Indicator (RI)
Paired Unicode letters (U+1F1E6 to U+1F1FF) that form country flag emoji when combined according to ISO 3166-1 alpha-2 codes. Symbol pairs (U+1F1E6โU+1F1FF):
// Match flag emoji (two regional indicator letters)
const flagRegex = /[\u{1F1E6}-\u{1F1FF}]{2}/gu;
'I am from ๐ฉ๐ช and you from ๐บ๐ธ'.match(flagRegex);
// ['๐ฉ๐ช', '๐บ๐ธ']
import regex
flag_pattern = regex.compile(r'[\U0001F1E6-\U0001F1FF]{2}')
flag_pattern.findall('Visiting ๐ฏ๐ต and ๐ฐ๐ท')
# ['๐ฏ๐ต', '๐ฐ๐ท']
Keycap Sequences
Keycaps like 0๏ธโฃ through 9๏ธโฃ follow the pattern: digit + U+FE0F + U+20E3:
const keycapRegex = /[0-9#*]\uFE0F\u20E3/gu;
'Press 1๏ธโฃ or 2๏ธโฃ'.match(keycapRegex);
// ['1๏ธโฃ', '2๏ธโฃ']
Common Mistakes
Mistake 1: Using . without the u flag in JavaScript. It matches one code unit, splitting emoji.
Mistake 2: Checking str.length > 0 to detect emoji content. An emoji-only string can have .length of 8 or more.
Mistake 3: Using character class ranges like [\u0080-\uFFFF] โ this misses most modern emoji above U+FFFF and produces false positives for non-emoji Unicode characters.
Mistake 4: Forgetting variation selectorVariation Selector (VS)
Unicode characters (VS-15 U+FE0E and VS-16 U+FE0F) that modify whether a character renders in text (monochrome) or emoji (colorful) presentation. U+FE0F. The character โค (U+2764) without VS16 is a text symbol; โค๏ธ with U+FE0F is the emoji presentationEmoji Presentation
The default rendering of a character as a colorful emoji glyph, either inherently or when triggered by Variation Selector-16..
Testing Your Pattern
Use our Sequence Analyzer to inspect any emoji's code points, then test your regex against it to verify full matches. Always test against ZWJ sequences, skin tone variants, and flag emoji before shipping emoji-handling code.