Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace unicode characters with characters (Javascript)

Take for example the following string:

“A profile of Mr. T, the A Team’s most well known member.”

How do I use javascript replace the unicode character encodings and convert that to the following:

"A profile of Mr. T, the A Team's most well known member."

like image 528
user2129607 Avatar asked Sep 18 '25 09:09

user2129607


1 Answers

@adeneo posted an option using jQuery. Here's a relevant answer I found that doesn't use jQuery. From this answer: What's the right way to decode a string that has special HTML entities in it?

function parseHtmlEnteties(str) {
    return str.replace(/&#([0-9]{1,4});/gi, function(match, numStr) {
        var num = parseInt(numStr, 10); // read num as normal number
        return String.fromCharCode(num);
    });
}
like image 71
user2129607 Avatar answered Sep 20 '25 23:09

user2129607