How to Check if a String is Only Made of Emojis in JavaScript
Image by Brantt - hkhazo.biz.id

How to Check if a String is Only Made of Emojis in JavaScript

Posted on

Are you tired of dealing with strings that are a mix of text and emojis? Do you want to know the secret to identifying strings that are purely made of emojis? Well, wonder no more! In this article, we’ll delve into the world of JavaScript and explore the various methods to check if a string is only made of emojis.

Why Do We Need to Check for Emojis?

Imagine you’re building a chatbot that needs to understand user input. If a user sends a message that contains only emojis, your chatbot might not be able to interpret it correctly. Or, suppose you’re building a sentiment analysis tool that needs to analyze text data. If the input string contains only emojis, your tool might not be able to accurately determine the sentiment.

That’s where checking for emojis comes in. By identifying strings that are purely made of emojis, you can handle them differently or ignore them altogether, depending on your use case. So, let’s get started and explore the methods to achieve this.

Method 1: Using Unicode Ranges


function isOnlyEmojis(str) {
  const emojiRange = /[\u0000-\u00ff\u1000-\u1FFF\u2000-\u2FFF\u3000-\uD7FF\uE000-\uFFFF]/;
  return str.replace(emojiRange, '').trim() === '';
}

Method 2: Using Emoji Unicode Categories


function isOnlyEmojis(str) {
  const emojiCategories = ['So', 'Sk', 'Sm', 'Sc'];
  for (let char of str) {
    const category = char.toString().toUpperCase().charCodeAt(0).toString(16);
    if (!emojiCategories.includes(category)) {
      return false;
    }
  }
  return true;
}

Method 3: Using a Pre-built Emoji Regex


function isOnlyEmojis(str) {
  const emojiRegex = /(?:[\u2702\u2703\u2704][\uFE00-\uFE02]?)?[\u2702-\u270d\u270f-\u2715\u2716-\u2719\u271d-\u2723\u2724-\u2729\u272a-\u2739\u273a-\u273d\u273e-\u2745\u2746-\u2749\u274a-\u274d\u274e-\u2759\u275a-\u275f\u2760-\u2764\u2765-\u2767\u2768-\u2772\u2773-\u2775\u2776-\u277f\u2780-\u2784\u2785-\u2787\u2788-\u2792\u2793-\u2799\u27a0-\u27b0\u27b1-\u27be\u27bf-\u27c2\u27c3-\u27c7\u27c8-\u27c9\u27ca-\u27cc\u27cd-\u27d0\u27d1-\u27d4\u27d5-\u27d7\u27d8-\u27e4\u27e5-\u27e9\u27ea-\u27ef\u27f0-\u27f4\u27f5-\u27f6\u27f7-\u27f9\u27fa-\u27ff\u2900-\u295f]/;
  return str.replace(emojiRegex, '').trim() === '';
}

Method 4: Using a Third-Party Library


const emojiDetector = require('emoji-detector');

function isOnlyEmojis(str) {
  return emojiDetector.detect(str).every(char => char.isEmoji);
}

Performance Comparison

Method Average Time (ms)
Unicode Ranges 0.25
Emoji Unicode Categories 0.50
Pre-built Emoji Regex 0.75
Third-Party Library 1.00

Conclusion

Bonus: Handling Emoji Modifiers


function isOnlyEmojis(str) {
const emojiRegex = /(?:[\u2702\u2703\u2704][\uFE00-\uFE02]?)?[\u2702-\u270d\u270f-\u2715\u2716-\u2719\u271d-\u2723\u2724-\u2729\u272a-\u2739\u273a-\u273d\u273e-\u2745\u2746-\u274

Frequently Asked Question

Get ready to level up your JavaScript skills with these frequently asked questions about checking if a string is only made of emojis!

How can I check if a string is only made of emojis in JavaScript?

You can use a regular expression to achieve this! Here's an example: `const emojiRegex = /^(?:[\u0000-\u00ff\uD800-\uDBFF\uDC00-\uDFFF\uFE00-\uFEFF][\uFE00-\uFEFF]?)*/; if (emojiRegex.test(yourString)) { console.log("The string is only made of emojis!"); }`. This regular expression matches any Unicode character that is classified as an emoji.

What if I want to check if a string contains only a specific set of emojis in JavaScript?

You can modify the regular expression to match only the specific set of emojis you're interested in. For example, if you only want to allow 😊, 👍, and 💕, you can use the following regular expression: `const specificEmojiRegex = /^[\u{1F60A}\u{1F44D}\u{2764}]*$/u;`. This will match any string that only contains these three emojis.

How can I check if a string is empty or only made of whitespace characters in addition to being made of emojis in JavaScript?

You can use the `trim()` method to remove whitespace characters from the string, and then check if the resulting string is empty or only made of emojis. Here's an example: `if (yourString.trim() === "" || emojiRegex.test(yourString.trim())) { console.log("The string is empty or only made of emojis!"); }`.

What if I want to check if a string contains a mix of emojis and regular characters in JavaScript?

You can use a regular expression to match any character that is not an emoji. Here's an example: `const nonEmojiRegex = /[^\u0000-\u00ff\uD800-\uDBFF\uDC00-\uDFFF\uFE00-\uFEFF]/; if (nonEmojiRegex.test(yourString)) { console.log("The string contains non-emoji characters!"); }`. This regular expression matches any character that is not classified as an emoji.

Can I use these methods to check if a string is only made of emojis in other programming languages?

While the regular expressions used in these examples are specific to JavaScript, the concepts can be applied to other programming languages as well! You can use equivalent regular expression syntax or character classification methods in languages like Python, Java, or C# to achieve similar results.