Emojis in Javascript

Kevin Scott
Nov 5, 2016 · 7 min read

Parsing emoji in Javascript is… not easy.

My friends and I built Emoji Salad, an Emoji Pictionary game played via SMS. Our backend is built in Node.js, and core game functionality requires parsing strings that contain emoji.

This article is a collection of the research I did while getting up to speed on emoji in Javascript. First, a quick dive into the inner workings of Unicode, followed by how emoji in particular are represented in JavaScript. Finally, let’s walk through writing a regular expression designed to handle all manner of emoji, heavily inspired by lodash’s implementation of split.

If your eyes glaze over like mine did while reading this, I put all the code into an npm library you can find here.

A digression into Unicode

The terms you want to digest are the following:

  • Code pointA numerical representation of a specific Unicode character.
  • Character Code — Another name for a code point.
  • Code Unit — An encoding of a code point, measured in bits. Javascript uses UTF-16.
  • Decimal — A way to represent code points in base 10.
  • Hexadecimal — A way to represent code points in base 16.

Let’s demonstrate with an example. Take as our specimen, the letter A.

Sad sack A. Cheer up bud, we’re about to turn you into a code point!

The letter A is represented by the code point 65 (in decimal), or 41 (in hexadecimal).

codePointAt and fromCodePoint are new methods introduced in ES2015 that can handle unicode characters whose UTF-16 encoding is greater than 16 bits, which includes emojis. Use these instead of charCodeAt, which doesn’t handle emoji correctly.

Here’s an example of using these methods, courtesy of xahlee.info:

); // prints 55357, WRONG!

); // prints 128568, correct

I will be using hexadecimal representations (\u0041) from now on, because our future regex will be built that way. A few things to note about hexadecimal representation in Javascript:

All hexadecimal code points must be 4 characters.

If the character code is less than 4 characters, it must be left padded with zeros.

// This is invalid
// This is valid

All hexadecimal code points are case insensitive.

// these are equivalent

They can be notated in two forms

In Javascript, hexadecimal can be represented in two ways: \u0041 and 0x0041. Jump into your browser console and you’ll see the following are equivalent:

> 'A'
> 'A'

Back to Emojis

Originally, the range of code points was 16 bits, which encompassed the English alphabet (now known as the Basic Multilingual Plane). Now, in addition to that original range, there are 16 more planes (17 total) to choose from.

The rest of the planes beyond the BMP are referred to as the “astral planes”, which include emoji. Emoji live on Plane 1, the Supplementary Multilingual Plane.

And the Consortium said, let there be emoji

What do you think the following will produce?


If you said 1, you are mistaken my friend! The correct answer is 2.

In Javascript, a string is a sequence of 16-bit code points. Since emoji are encoded above the BMP, it means that they are represented by a pair of code points, also known as a surrogate pair.

So for instance, 0x1F600, which is 😀, is represented by:


(The first pair is called the lead surrogate, and the latter the tail surrogate.)

Go ahead and copy that surrogate pair into your browser, and you’ll see 😀. Javascript interprets this pair of characters as having a length of 2. That’s why you can’t just do something like:

>["a", "b", "c", "�", "�"]

So, how do we get the surrogate pair? There’s a great explanation here, and here’s a gist illustrating going from emoji to decimal to surrogate pair and back again:

Because of these limitations within Javascript, in order to parse strings containing emoji, we need some fancy footwork.

Writing a regular expression

Luckily, the internet is awash in smarter folks than I. The lodash library has produced a rock solid emoji regular expression. Is is:


Woof, that’s a monster! Still, we’re enterprising programmers, we’re not afraid of a little regex, right? Let’s reverse engineer this.

From the Wikipedia emoji entry, there’s a couple ranges of emoji (many of which have unassigned values, presumably for future emoji):

To make this easier, I’m assuming anything in those ranges is emoji. Our audience uses the English alphabet over SMS, so tough luck if I trawl up any other unsuspecting characters.


They range from U+2700 to U+27BF, so the regular expression for that looks like:

> true

Miscellaneous Symbols and Pictographs

These range from U+1F300 to U+1F5FF, with the following surrogate pairs:

> "\uD83C\uDF00"
> "\uD83D\uDDFF"

The regex for this range, from lodash’s implementation, is:

> true
> true

Supplemental Symbols and Pictographs

From U+1F900 to U+1F9FF, with the following surrogate pairs:

> "\uD83E\uDD10"
> "\uD83E\uDDC0"

We can reuse the same regex as above:

> true
> true


From U+1F600 to U+1F64F, with surrogate pairs:

> "\uD83D\uDE00"
> "\uD83D\uDE4F"

Also covered by that same regex:

> true
> true

Transport and Map Symbols

Includes U+1F680 to U+1F6FF, with surrogate pairs:

> "\uD83D\uDE80"
> "\uD83D\uDEFF"

Also covered by that same regex:

> true
> true

Miscellaneous Symbols

Includes U+2600 to U+26FF, with surrogate pairs:

> "\u2600"
> "\u26FF"

We can write a regex for this like so:

> true
> true

lodash’s mysterious other regex

There’s another section in the beginning of that original lodash regex we haven’t looked at yet:


If we examine what those characters represent, we get:

> "🇦"
> "🇿"

Holy camoley, what the heck are those? I’ll tell you what those are: those are the regional indicator symbol letters A-Z. These are used to create flags for various countries. For instance:

> "🇺"
> "🇸"
// when combining "u" + "s":
"\ud83c\uddfa" + "\ud83c\uddf8"
> "🇺🇸"

So that’s a good section to keep around. The regex so far is:


Let’s test it out

I’m relying on Emoji-data’s json to provide a library of every emoji. When we run this regular expression against that list, we get 746 matches, 99 misses. Let’s go through the misses:


There are 12 keycap emojis (#️⃣️, *️⃣ and 0️⃣️9️⃣️), which look like:

> "0️⃣️"
> "9️⃣"
> "#️⃣"
> "*️⃣"

(That middle “\uFE0F’ is optional, by the way.)

These are covered by the following:

> true
> true

Other Miscellaneous Emoji

Towards the bottom of the Unicode Block Emoji entry on Wikipedia is the following:

Additional emoji can be found in the following Unicode blocks: Arrows (8 codepoints considered emoji), Basic Latin (12), CJK Symbols and Punctuation (2), Enclosed Alphanumeric Supplement(41), Enclosed Alphanumerics (1), Enclosed CJK Letters and Months (2), Enclosed Ideographic Supplement (15), General Punctuation (2), Geometric Shapes (8), Latin-1 Supplement (2), Letterlike Symbols (2), Mahjong Tiles (1), Miscellaneous Symbols and Arrows (7), Miscellaneous Technical (18), Playing Cards (1), and Supplemental Arrows-B (2).

Why the heck are these other random emoji scattered around like detritus? I believe the reason is: “because of history”. But I don’t really know. If you know, leave a comment and educate us all!

I won’t go through these one by one. You can look in my Github repo for a breakdown of the regex for each block. Suffice to say the regex that covers all these pesky buggers is:



Which means that… drum roll… the final regex for parsing emojis is:


Hopefully that dispells some of the confusion around parsing emoji.

React Native Cafe

Articles about React Native

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store