'How to convert string with emoji, convert emoji to string like this url with regex

How to convert string with emoji ❤️😬, convert emoji to string like this question url %EF%B8%8F with regex?

function emojitourl(str) {

return str;
}


var str = 'Thats a 😆 😛';
console.log( emojitourl(str) ); // Thats a ??


Solution 1:[1]

I guess this is what you want. This page helped me to sort out the ranges for the emoji characters. Apparently emoji characters are represented by two successive UTF16 characters called surrogate pairs. As a side info, each emoji character increase the length value of a string by 2.

function emojiToUnicode(s) {
return s.match(/\ud83c[\udf00-\udfff]|\ud83d[\udc00-\ude4f]|\ud83d[\ude80-\udeff]/g)
        .map( e => "\\u" + e.charCodeAt(0).toString(16) + "\\u" + e.charCodeAt(1).toString(16))
}

var str = 'Thats a ? ?';
document.write('<pre>' + JSON.stringify(emojiToUnicode(str), 0, 2) + '</pre>');

Solution 2:[2]

I realise that I'm a bit late to the party here, but as a follow on from Redu's answer, here is the updated regex to ensure you grab even more emoji's.

function emojiToUnicode(s) {
    return s.match(/([\u2700-\u27BF]|[\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2011-\u26FF]|\uD83E[\uDD10-\uDDFF])/g)
        .map( e => "\\u" + e.charCodeAt(0).toString(16) + "\\u" + e.charCodeAt(1).toString(16))
}

and I also wanted to add that if you would like to keep the original string with just the emoji's replaced with unicode, you should do something like this:

function emojiToUnicode(str) {
    return str.replace(/([\u2700-\u27BF]|[\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2011-\u26FF]|\uD83E[\uDD10-\uDDFF])/g, function(e) {
        return "\\u" + e.charCodeAt(0).toString(16) + "\\u" + e.charCodeAt(1).toString(16);
    });
}

(Credit for the updated regex search: https://stackoverflow.com/a/41543705/6756350)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Luke