简体   繁体   English

如何用 JavaScript 中的相应字母替换 Unicode 字母表情符号?

[英]How can I replace Unicode letter emojis with corresponding letter in JavaScript?

I have a string, for example: " cding question".我有一个字符串,例如:“cding question”。

I want to replace the "", "", and all Unicode letter emojis for that matter into the corresponding letter like "a", "o", etc.我想将“”、“”和所有 Unicode 字母表情符号替换为相应的字母,如“a”、“o”等。

*Note: Unsure if this helps, but I believe the Unicode letter emojis are referred to as regional indicator symbols. *注意:不确定这是否有帮助,但我相信 Unicode 字母表情符号被称为区域指示符号。

This is the solution I ended up figuring out thanks to some direction by ktp.由于ktp的一些指导,这是我最终弄清楚的解决方案。 It may not be the most efficient, but it was the best I could figure out for the time being.这可能不是最有效的,但这是我暂时能想到的最好的。 Open to feedback.接受反馈。

//Loop through each character of the text
for (let i = 0; i < text.length; i++) {
    //Check if the character is a emoji
    if (text.charCodeAt(i) >= 255)
        //Check if the character is 🇦 or between 🇿 
        if (text.codePointAt(i) >= 127462 && text.codePointAt(i) <= 127487)
            //Set the text equal to text with the emoji replaced as a lowercase letter equivalent
            text =
                text.substr(0, i) +
                String.fromCodePoint(text.codePointAt(i) - 127365) +
                text.substr(i + 2);
}

is this similar to what you are looking for?:这与您正在寻找的相似吗?:

let char = '🇦';
let cp = char.codePointAt(0);
let newChar = String.fromCodePoint(cp % 256);
console.log(newChar);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript中检测字母字符? - How can I detect a letter character in JavaScript? 如何用javascript生成一个字母 - how can i generate one letter with javascript 我如何在javascript中匹配和替换字符串及其前一个字母? - How can i match and replace a string and its before one letter in javascript? 在javascript中,如何用javascript对象中的另一个字母替换一个字母? - In javascript, how to replace a letter with another in javascript object? 正确猜出字母后,如何用字母替换占位符? - How can I replace a letter placeholder with a letter when it's been correctly guessed? 如何用数字替换字符串中字母的出现 - How can I replace the occurrence of a letter within a string with a number 如何将 unicode 字符转换为相应的表情符号? - How to convert unicode characters into corresponding emojis? 使用RegEx如何匹配javaScript中字符串中的字母数字字母模式? - Using RegEx how can I match a pattern of letter number letter in a string in javaScript? 如何创建目录列表,使用javascript将项目的每个首字母排序到一个字母组 - How can I create a Directory listing that sorts each first letter of a item to a letter-group with javascript 如何使用 Javascript 处理每个文本字母? - How can I process each letter of text using Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM