Littlebobbydroptables
Littlebobbydroptables

Reputation: 3731

Twemoji parse string and change emojis to code equivalent

I am using this library https://www.npmjs.com/package/twemoji and can't figure out how to convert string like this

'I \u2764\uFE0F emoji!'

not to

I <img class="emoji" draggable="false" alt="❤️" src="/assets/36x36/2764.gif"> emoji!

but to

I :2764: emoji!

i've tried their twemoji.convert.toCodePoint() helper, tried it like this

twemoji.convert.toCodePoint('I \u2764\uFE0F emoji!');

but result is

49-20-2764-fe0f-20-65-6d-6f-6a-69-21

it have needed part (2764), but also converted all letters too. So i need like a regexp to find an emoji in text, but all i find is

var ranges = [
  '\ud83c[\udf00-\udfff]', // U+1F300 to U+1F3FF
  '\ud83d[\udc00-\ude4f]', // U+1F400 to U+1F64F
  '\ud83d[\ude80-\udeff]'  // U+1F680 to U+1F6FF
];

from here http://crocodillon.com/blog/parsing-emoji-unicode-in-javascript but it doesn't covers all of emojies (no flags for example)

Upvotes: 0

Views: 1534

Answers (1)

Littlebobbydroptables
Littlebobbydroptables

Reputation: 3731

After checking library source code, found simple solution:

    var text = twemoji.replace(text, function(emoji) {
        return twemoji.convert.toCodePoint(emoji);
    });

Upvotes: 1

Related Questions