Mina
Mina

Reputation: 177

How do I convert Unicode to emoji in JavaScript?

I have an array with Unicode data like var data = ["1F923", "1F603"]

If I use "\u{1F923}" in console, it returns an emoji, but if I use "\u{"+ data[0] +"}", it returns an error. Can anyone please help me to convert Unicode to emoji.

Upvotes: 3

Views: 13808

Answers (3)

user2226755
user2226755

Reputation: 13157

You can convert hexa string representation to int with parseInt("1f47f", 16) and then convert, you convert from character index to string unicode with String.fromCodePoint(1234).

const unicode = ["1f47f", "1f601", "1f60b", "1f62c", "1f9d0", "1f47f", "1f600", "1f601", "1f602", "1f603", "1f604", "1f605", "1f606", "1f607", "1f608", "1f609", "1f60a", "1f60b", "1f60c", "1f60d", "1f60e", "1f60f", "1f610", "1f611", "1f612", "1f613", "1f614", "1f615", "1f616", "1f617", "1f618", "1f619", "1f61a", "1f61b", "1f61c", "1f61d", "1f61e", "1f61f", "1f620", "1f621", "1f622", "1f623", "1f624", "1f625", "1f626", "1f627", "1f628", "1f629", "1f62a", "1f62b", "1f62c", "1f62d", "1f62e", "1f62f", "1f630", "1f631", "1f632", "1f633", "1f634", "1f635", "1f636", "1f637", "1f641", "1f642", "1f643", "1f644", "1f910", "1f911", "1f912", "1f913", "1f914", "1f915", "1f917", "1f922", "1f923", "1f924", "1f925", "1f927", "1f928", "1f929", "1f92a", "1f92b", "1f92c", "1f92d", "1f92e", "1f92f", "1f970", "1f973", "1f974", "1f975", "1f976", "1f97a", "2639", "263a"]


unicode.forEach((val) => {
  document.body.innerHTML += String.fromCodePoint(parseInt(val, 16))
});

Upvotes: -1

Haktan Enes Biçer
Haktan Enes Biçer

Reputation: 682

You can use this JavaScript function for converting Unicode to emoji

var unicodeToChar = function(text) {
  return text.replace(/\\u[\dA-F]{4}/gi, function(match) {
      return String.fromCharCode(parseInt(match.replace(/\\u/g, ''), 16));
  });
}

new version

var emojiToUnicode = function(text) {
    var emojiUnicodes = '';
    var i;
    var matchedText = text.match(regex);
    if (matchedText != null) {
        var unicodes = text.match(regex)
            .map(e => "\\u" + e.charCodeAt(0).toString(16) + "\\u" + e.charCodeAt(1).toString(16));
        for (i = 0; i < unicodes.length; i++) {
            emojiUnicodes = emojiUnicodes + unicodes[i]
        }
        text = text.replace(regex, '') + emojiUnicodes.replace(/\\"/g, '\"');
    }
    return text;
}

You can find sample regexes by searching the following code snippet on GitHub.

 /(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}

If you want to convert Unicode to emoji, this is the function

var unicodeToChar = function(text) {
    return text.replace(/\\u[\dA-F]{4}/gi, function(match) {
        return String.fromCharCode(parseInt(match.replace(/\\u/g, ''), 16));
    });
}

Upvotes: 6

RalfFriedl
RalfFriedl

Reputation: 1130

Emoji is a subset of unicode. There is no conversion from unicode to emoji necessary or possible. Just change your array to

var data = ["\u{1F923}", "\u{1F603}"]

If your input is a hex number, you can use

String.fromCodePoint(parseInt ("1F923", 16))

In HTML you can also use HTML hex entities

"&#x" + "1F923" + ";"

Upvotes: 18

Related Questions