JavaScript

How to check if a string contains emojis in JavaScript?

Advertisements

If you have user-generated content in your web application, chances are you have to deal with strings containing emojis. Since emojis are stored as Unicode, chances are that we want to detect these in our code and then render them accordingly. This article discusses how we can check if a string contains emojis in JavaScript.

A Unicode representation of emojis looks something like this:

'\u{1F60A}' // "😊"
'\u{1F42A}' // "πŸͺ"
JavaScript

And JavaScript regular expressions have a Unicode mode now, so we can use Unicode property escapes to check for various things like emojis and currency symbols.

const emojiRegex = /\p{Emoji}/u;
emojiRegex.test('😊'); // true
JavaScript

And it has good browser support as well:

And if we wanted to do a replace, we can do:

'replacing emojis 😊πŸͺ'.replaceAll(/\p{Emoji}/ug, '');

// 'replacing emojis'
JavaScript

The β€œg” flag was used to replace all emojis.

It is worth noting that some emojis are a combination of multiple emojis (or code points). So, this is not a fail-safe approach and there can be some more nuances:

"πŸ‡―πŸ‡΅".replaceAll(/\p{Emoji}/gu, '-'); // '--'
"πŸ™‹πŸΏ".replaceAll(/\p{Emoji}/gu, '-'); // '--'
"πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦".replaceAll(/\p{Emoji}/gu, '-'); // '----'
JavaScript

The country flags are a combination of regional symbol indicator letters” (πŸ‡― + πŸ‡΅), the emoji followed by a skin tone modifier is again a combination, and the family one is a combination of 4 different emojis.

Saransh Kataria

Born in Delhi, India, Saransh Kataria is the brain behind Wisdom Geek. Currently, Saransh is a software developer at a reputed firm in Austin, and he likes playing with new technologies to explore different possibilities. He holds an engineering degree in Computer Science. He also shares his passion for sharing knowledge as the community lead at Facebook Developer Circle Delhi, NCRΒ which is a developer community in Delhi, India.

Share
Published by
Saransh Kataria
Tags: javascript

Recent Posts

How To Get The Hash of A File In Node.js

While working on a project, I wanted to do an integrity check of a file…

21 hours ago

Native popover API in HTML

Popovers have been a problem that was typically solved by using a third-party solution. But…

1 week ago

Node.js 20.6 adds built-in support for .env files

Node.js 20.6 added built-in support for the .env file. This is an excellent addition to the platform…

2 weeks ago

Object destructuring in TypeScript

Object destructuring is a powerful ES 6 feature that can help developers write cleaner code.…

4 weeks ago

Improve git clone performance in a CI pipeline

Have you felt particularly annoyed by the time it takes to clone a large repository,…

1 month ago

Fix: Hydration failed because the initial UI does not match what was rendered on the server

Within a React or Next.js app, if you encounter the error "Hydration failed because the…

1 month ago
Advertisements