用字符替换unicode字符(Javascript)

时间:2015-06-17 22:35:41

标签: javascript unicode unicode-string

以下面的字符串为例:

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

如何使用javascript替换unicode字符编码并将其转换为以下内容:

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

1 个答案:

答案 0 :(得分:2)

@adeneo使用jQuery发布了一个选项。这是我发现不使用jQuery的相关答案。从这个回答: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);
    });
}