【问题标题】:How can I replace Unicode letter emojis with corresponding letter in JavaScript?如何用 JavaScript 中的相应字母替换 Unicode 字母表情符号?
【发布时间】:2021-04-09 06:40:31
【问题描述】:

我有一个字符串,例如:“???? c????ding question”。

我想将“????”、“????”和所有 Unicode 字母表情符号替换为相应的字母,如“a”、“o”等。

*注意:不确定这是否有帮助,但我相信 Unicode 字母表情符号被称为区域指示符。

【问题讨论】:

    标签: javascript string unicode emoji


    【解决方案1】:

    这与您正在寻找的相似吗?:

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

    【讨论】:

    • 记录newChar时,控制台中的值为æ。但是,我确实认为您对这种方法有所了解。我会尝试更多地研究它。
    【解决方案2】:

    多亏了 ktp 的一些指导,我最终找到了解决方案。这可能不是最有效的,但这是我暂时能想到的最好的。接受反馈。

    //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);
    }
    

    【讨论】:

    • 一项优化:删除if (text.charCodeAt(i) &gt;= 255)
    猜你喜欢
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 2018-03-19
    • 2020-03-16
    • 2015-04-15
    • 2021-05-27
    • 1970-01-01
    • 2021-03-28
    相关资源
    最近更新 更多