Radhika
Radhika

Reputation: 533

Convert Unicode string to display emoji character

Here is my unicode string that is I am fetching from DB in jquery using API

\ud83d\ude02\u263a\ufe0f\u263a\ufe0f\ud83d\ude0a\ud83d\ude09\ud83d\ude0f\ud83d\ude01\ud83d\ude18\ud83d\ude14\ud83d\ude18\ud83d\ude01\ud83d\ude02\ud83d\ude04\ud83e\udd23\ud83d\ude02\u263a\ufe0f\ud83d\ude0f\ud83d\ude0f

and I want to display it as like emoji character. If I write this string explicitly in script then it is displaying emoji but as I am fetching it from DB and displaying it from response I get from API it is displayed as plain string.

$.each(response.data,function(key,value){
        const str = value.comment_text.replace(/\\\\/g,"\\");
        html += '<div class="comment-block" id="cmnt' + value.comment_id + '">\
                    <div class="content-box">\
                        <p class="text long-string "><a href="javascript:void(0)" class="blue-text bold-font">' + value.first_name + ' ' + value.last_name.substr(0,1).toUpperCase() + ' </a> #post test  #post1  #post3 hvh\ud83d\ude0f "' + str + '"</p>\
                    </div>\
                </div>';
    });        

this is my code in js

\\\\ud83d\\\\ude02\\\\u263a\\\\ufe0f\\\\u263a\\\\ufe0f\\\\ud83d\\\\ude0a\\\\ud83d\\\\ude09\\\\ud83d\\\\ude0f\\\\ud83d\\\\ude01\\\\ud83d\\\\ude18\\\\ud83d\\\\ude14\\\\ud83d\\\\ude18\\\\ud83d\\\\ude01\\\\ud83d\\\\ude02\\\\ud83d\\\\ude04\\\\ud83e\\\\udd23\\\\ud83d\\\\ude02\\\\u263a\\\\ufe0f\\\\ud83d\\\\ude0f\\\\ud83d\\\\ude0f

string in DB

Upvotes: 0

Views: 3782

Answers (1)

Roshan Patil
Roshan Patil

Reputation: 196

Below function takes all unicode characters in the inputted string, and converts them to the character.

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

Upvotes: 5

Related Questions