0

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 ??
General Grievance
  • 4,555
  • 31
  • 31
  • 45
user1575921
  • 1,078
  • 1
  • 16
  • 29
  • 1
    Aren't the emoji in your `str` variable already unicode? Your function is not converting them to unicode, it's URL-encoding them, which is something different altogether. What exactly is it you are trying to accomplish? –  Apr 17 '16 at 11:24
  • @torazaburo thanks for reply I wanto convert the emoji icon to code like this question url – user1575921 Apr 19 '16 at 08:12

2 Answers2

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>');
Redu
  • 25,060
  • 6
  • 56
  • 76
  • thanks for reply, I user yours make https://jsfiddle.net/geqc21u0/ but theres error `SyntaxError: Unexpected token '>'` ? – user1575921 Apr 19 '16 at 08:07
  • Thanks for the link reference. the emoji represent in this question url %EF%B8%8F is "utf16" ? – user1575921 Apr 19 '16 at 08:20
  • @user1575921 i have checked it should be working please retry and i can not see any %EF%B8%8F in this question url. – Redu Apr 19 '16 at 12:16
0

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)

Luke
  • 65
  • 1
  • 9