Emoji Detection in Text Strings: Algorithms and Libraries

Emoji Detection in Text Strings

Detecting whether a string contains emojis—and extracting them accurately—is harder than it looks. The Unicode standard has grown to include over 3,700 emoji characters spread across multiple code point ranges, with new ones added every year. A naive approach using a fixed range check will miss most of them.

This guide covers the algorithms, Unicode properties, and production-ready libraries you need to detect emojis reliably.

Why Simple Range Checks Fail

A common first attempt is checking whether a code point falls in the range U+1F600–U+1F64F (Emoticons block). This catches classics like 😀, 😂, and 😎, but misses:

  • Basic emoji: ©️ (U+00A9), ® (U+00AE), ™️ (U+2122) — in the Latin Extended range
  • Dingbats: ✅ (U+2705), ❌ (U+274C) — in the Dingbats block
  • Enclosed alphanumerics: 🅰️, 🅱️
  • ZWJज़ीरो विड्थ जॉइनर (ZWJ)
    एक अदृश्य Unicode वर्ण (U+200D) जिसका उपयोग कई इमोजी को एक संयुक्त इमोजी में जोड़ने के लिए किया जाता है, जैसे लोगों और वस्तुओं को पेशे वाले इमोजी में संयोजित करना।
    sequences
    : 👨‍💻 (man technologist) — multiple code points joined by U+200D
  • Keycap sequences: 1️⃣ — digit + variation selector + combining enclosing keycap
  • Flag sequences: 🇺🇸 — pairs of Regional Indicator letters

The only reliable approach is to use the official Unicode emoji property data.

The Unicode Emoji Properties Approach

Unicode defines several properties relevant to emoji detection, published in emoji-data.txt from the Unicode Character Database (UCD):

Property Meaning
Emoji The code point is an emoji
Emoji_Presentation Displayed as emoji by default (not text)
Emoji_Modifier A skin tone modifier (🏻–🏿)
Emoji_Modifier_Base Can be modified by a skin tone modifier
Emoji_Component Used in emoji sequences (ZWJ, keycap, etc.)
Extended_Pictographic Broader set including reserved ranges

For most detection tasks you want Extended_Pictographic, which includes current emoji plus code points reserved for future emoji assignments.

Detection in Python

Using the emoji Library

The emoji library maintains an up-to-date Unicode dataset:

import emoji

text = "Hello 👋 world! Check this out 🚀"

# Check if string contains any emoji
has_emoji = emoji.emoji_count(text) > 0
print(has_emoji)  # True

# Count emojis
count = emoji.emoji_count(text)
print(count)  # 2

# Extract emoji with positions
for item in emoji.emoji_list(text):
    print(item)
# {'match_start': 6, 'match_end': 7, 'emoji': '👋'}
# {'match_start': 26, 'match_end': 27, 'emoji': '🚀'}

# Replace emojis
clean = emoji.replace_emoji(text, replace="")
print(clean)  # "Hello  world! Check this out "

Using the regex Module with Unicode Properties

The third-party regex module (not the built-in re) supports Unicode properties:

import regex

# Match any Extended_Pictographic character or emoji sequence
EMOJI_PATTERN = regex.compile(
    r'\p{Extended_Pictographic}'
    r'(?:\uFE0F)?'           # optional variation selector-16
    r'(?:\u20E3)?'           # optional combining enclosing keycap
    r'(?:\uFE0F\u20E3)?'     # keycap sequence
    r'(?:\u200D\p{Extended_Pictographic}(?:\uFE0F)?)*'  # ZWJ sequences
    r'(?:[\U0001F1E0-\U0001F1FF]{2})?',  # flag sequences
    regex.UNICODE
)

text = "Deploying 🚀 to production 👨‍💻 — fingers crossed 🤞🏽"
matches = EMOJI_PATTERN.findall(text)
print(matches)  # ['🚀', '👨‍💻', '🤞🏽']

Pure stdlib with unicodedata

For simpler cases without extra dependencies, check the unicodedata category:

import unicodedata

def contains_emoji_simple(text: str) -> bool:
    for char in text:
        cat = unicodedata.category(char)
        # So (Symbol, other) covers many but not all emoji
        if cat == "So":
            return True
    return False

This is fast but incomplete — it misses many emoji that fall in other categories.

Detection in JavaScript

Using the emoji-regex Package

import emojiRegex from 'emoji-regex';

const regex = emojiRegex();
const text = "Meeting at 3pm 📅 — bring your laptop 💻";

// Test for presence
console.log(regex.test(text)); // true

// Extract all emojis
const matches = [...text.matchAll(regex)];
matches.forEach(m => {
  console.log(`Found: ${m[0]} at index ${m.index}`);
});
// Found: 📅 at index 15
// Found: 💻 at index 38

// Count
const count = [...text.matchAll(regex)].length;
console.log(count); // 2

Note that emoji-regex is generated directly from Unicode data, so it stays accurate across emoji versions.

Native Unicode Property Escapes (ES2018+)

Modern JavaScript engines support \p{} in regex with the u flag:

// Requires Node.js 10+ or modern browsers
const emojiRx = /\p{Emoji}/u;
const extPictoRx = /\p{Extended_Pictographic}/u;

console.log(emojiRx.test("Hello 🌍")); // true
console.log(extPictoRx.test("No emoji here")); // false

// Extract using matchAll
const text = "Status: ✅ Build passed, 🔴 Tests failed";
const allEmoji = [...text.matchAll(/\p{Extended_Pictographic}/gu)];
console.log(allEmoji.map(m => m[0])); // ['✅', '🔴']

Detection in Go

package main

import (
    "fmt"
    "unicode"
    "golang.org/x/text/unicode/rangetable"
)

// Basic check using unicode.Is
func containsEmoji(s string) bool {
    for _, r := range s {
        if unicode.Is(unicode.So, r) || // Symbol, other
           (r >= 0x1F600 && r <= 0x1FFFF) || // Supplemental symbols
           (r >= 0x2600 && r <= 0x27BF) {    // Misc symbols
            return true
        }
    }
    return false
}

func main() {
    texts := []string{
        "Hello world",
        "Rocket 🚀 launched",
        "©️ Copyright symbol",
    }
    for _, t := range texts {
        fmt.Printf("%q → %v\n", t, containsEmoji(t))
    }
}

For production Go code, consider the github.com/rivo/uniseg package, which handles grapheme cluster segmentation correctly and can identify emoji clusters.

Handling Edge Cases

Variation Selectors

Many emoji have both a text (VS15, U+FE0E) and emoji (VS16, U+FE0F) presentation. The digit ☎ can appear as ☎︎ (text) or ☎️ (emoji). Your detection should account for the variation selector:

phone_text = "\u260E\uFE0E"   # ☎︎  text presentation
phone_emoji = "\u260E\uFE0F"  # ☎️  emoji presentation

import emoji
print(emoji.emoji_count(phone_text))   # 0
print(emoji.emoji_count(phone_emoji))  # 1

ZWJ Sequences

👨‍💻 is a single grapheme cluster composed of 👨 + ZWJ (U+200D) + 💻. When counting or extracting emoji, treat ZWJ sequences as one unit. Libraries like emoji (Python) and emoji-regex (JS) handle this automatically.

Regional Indicator Flags

Country flags like 🇩🇪 consist of two Regional Indicator letters (U+1F1E6–U+1F1FF). They are only valid in pairs. A single 🇩 without a following 🇪 is not a flag.

Performance Considerations

For high-throughput text processing:

  1. Pre-compile your regex — do it once at module load, not per call
  2. Short-circuit on ASCII — if all bytes are < 128, there are no emoji (they are all non-ASCII)
  3. Use a library — regex-based approaches with proper Unicode support are faster than custom range tables you maintain yourself
def fast_has_emoji(text: str) -> bool:
    # Short-circuit: emoji require non-ASCII bytes in UTF-8UTF-8
एक परिवर्तनशील-चौड़ाई वाला Unicode एन्कोडिंग जो प्रति वर्ण 1 से 4 बाइट का उपयोग करता है, वेब पर प्रमुख (98%+ वेबसाइटों द्वारा उपयोग किया जाता है)।
if text.isascii(): return False return emoji.emoji_count(text) > 0

Explore More on EmojiFYI

संबंधित टूल्स

🔍 सीक्वेंस विश्लेषक सीक्वेंस विश्लेषक
ZWJ सीक्वेंस, स्किन टोन मॉडिफ़ायर, कीकैप सीक्वेंस और फ्लैग जोड़ों को अलग-अलग घटकों में डीकोड करें।

शब्दकोश के शब्द

Unicode मानक Unicode मानक
Unicode Consortium द्वारा बनाए रखी गई संपूर्ण वर्ण एन्कोडिंग प्रणाली, जो वर्ण, गुण, एल्गोरिदम और एन्कोडिंग रूपों को परिभाषित करती है।
UTF-8 UTF-8
एक परिवर्तनशील-चौड़ाई वाला Unicode एन्कोडिंग जो प्रति वर्ण 1 से 4 बाइट का उपयोग करता है, वेब पर प्रमुख (98%+ वेबसाइटों द्वारा उपयोग किया जाता है)।
इमोजी इमोजी
एक जापानी शब्द (絵文字) जिसका अर्थ है 'चित्र वर्ण' — छोटे ग्राफिकल प्रतीक जो डिजिटल संचार में विचार, भावनाएं और वस्तुएं व्यक्त करने के लिए उपयोग किए जाते हैं।
इमोजी प्रेजेंटेशन इमोजी प्रेजेंटेशन
किसी वर्ण का रंगीन इमोजी ग्लिफ के रूप में डिफ़ॉल्ट रेंडरिंग, चाहे स्वाभाविक रूप से या Variation Selector-16 द्वारा ट्रिगर होने पर।
इमोजी सीक्वेंस इमोजी सीक्वेंस
एक या अधिक Unicode कोड पॉइंट का क्रमबद्ध समूह जो मिलकर एक इमोजी वर्ण का प्रतिनिधित्व करते हैं।
कीकैप सीक्वेंस कीकैप सीक्वेंस
एक इमोजी सीक्वेंस जो किसी अंक या प्रतीक, उसके बाद VS-16 (U+FE0F) और कॉम्बिनिंग एनक्लोज़िंग कीकैप वर्ण (U+20E3) से बनती है।
कोड पॉइंट कोड पॉइंट
Unicode मानक में प्रत्येक वर्ण को दिया गया एक अद्वितीय संख्यात्मक मान, जो U+XXXX प्रारूप में लिखा जाता है (जैसे 😀 के लिए U+1F600)।
ग्राफ़ीम क्लस्टर ग्राफ़ीम क्लस्टर
एक उपयोगकर्ता-दृश्य वर्ण जो कई Unicode कोड पॉइंट से मिलकर बना हो सकता है, लेकिन एकल दृश्य इकाई के रूप में प्रदर्शित होता है।
ज़ीरो विड्थ जॉइनर (ZWJ) ज़ीरो विड्थ जॉइनर (ZWJ)
एक अदृश्य Unicode वर्ण (U+200D) जिसका उपयोग कई इमोजी को एक संयुक्त इमोजी में जोड़ने के लिए किया जाता है, जैसे लोगों और वस्तुओं को पेशे वाले इमोजी में संयोजित …
टेक्स्ट प्रेजेंटेशन टेक्स्ट प्रेजेंटेशन
किसी वर्ण का मोनोक्रोम टेक्स्ट प्रतीक के रूप में रेंडरिंग, चाहे डिफ़ॉल्ट रूप से या Variation Selector-15 लागू होने पर।
यूनिकोड यूनिकोड
एक सार्वभौमिक वर्ण एन्कोडिंग मानक जो इमोजी सहित सभी लेखन प्रणालियों और प्रतीक सेटों में प्रत्येक वर्ण को एक अद्वितीय संख्या प्रदान करता है।
रीजनल इंडिकेटर (RI) रीजनल इंडिकेटर (RI)
युग्मित Unicode अक्षर (U+1F1E6 से U+1F1FF) जो ISO 3166-1 alpha-2 कोड के अनुसार संयुक्त होने पर देश के झंडे वाले इमोजी बनाते हैं।
वेरिएशन सिलेक्टर (VS) वेरिएशन सिलेक्टर (VS)
Unicode वर्ण (VS-15 U+FE0E और VS-16 U+FE0F) जो यह निर्धारित करते हैं कि कोई वर्ण टेक्स्ट (मोनोक्रोम) या इमोजी (रंगीन) प्रस्तुति में रेंडर होगा।
स्किन टोन मॉडिफायर स्किन टोन मॉडिफायर
फिट्ज़पैट्रिक स्केल पर आधारित पाँच Unicode संशोधक वर्ण जो मानव इमोजी का त्वचा रंग बदलते हैं (U+1F3FB से U+1F3FF तक)।

संबंधित लेख