JavaScript 如何将 Unicode 十六进制字符转换为 Latin-1

How to convert Unicode Hex Characters to Latin-1 in JavaScript

我正在使用 rae-api 来获取他们词典中单词的定义。问题是例如:我搜索单词 hola 的定义,它 returns como salutación familiar.。我想用 Latin-1 字符获取 ó 的值:ó,因此,结果将是 como salutación familiar.

getHex函数将 &#; 和 returns xF3 删除到文本中。但是,我想将所有 Unicode 十六进制字符转换为 Latin-1。

我已经测试了很多类似问题的答案,但是 none 对我有用(例如:decodeURIComponent 或使用 Hex to utf8 库)。我正在使用 Discord.js.

userInput is the word to search for

const { RAE } = require("rae-api");
const rae = new RAE();
        
//-----------------  RAE  -------------------------

    
function getHex(text) {
   text = text.replace(/&#(.*?);/g, function (a, b) {
        //Get the Latin-1 value of b and return it to the text
        return b;
   })

   return text;
}

rae.searchWord(`${userInput}`).then(r => {
    let wordID = r.getRes()[0].getId();

rae.fetchWord(wordID).then(word => {
    let defs = word.getDefinitions();
    let definition = defs[0].getDefinition();
        
    return message.channel.send(`La definición de ${userInput} es: ${getHex(definition)}`);
})
    
}).catch(e => { return message.channel.send("No se encontró esa palabra!")})
    

var input = 'F3';
var decimalValue = parseInt(input, 16); // Base 16 or hexadecimal
var character = String.fromCharCode(decimalValue);
console.log('Input:', input);
console.log('Decimal value:', decimalValue);
console.log('Character representation:', character);