【发布时间】:2021-01-24 14:54:18
【问题描述】:
我正在使用 rae-api 来获取他们字典中单词的定义。问题例如:我搜索单词hola的定义,它返回como salutación familiar.。我想获取 ó 的 Latin-1 字符的值:ó,因此,结果将是 como salutación familiar. getHex 函数删除 &#; 并将 xF3 返回给文本。但是,我想将所有 Unicode 十六进制字符转换为 Latin-1。
我已经在类似问题中测试了很多答案,但它们都不适合我(例如:decodeURIComponent 或使用 Hex 到 utf8 库)。我正在使用 Discord.js。
userInput是要搜索的词
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!")})
【问题讨论】:
标签: javascript hex discord.js