简体   繁体   English

JavaScript:比较字符和重音字符

[英]JavaScript: Comparing characters and accented characters

I'm creating a simple hang man like game.我正在创建一个简单的像游戏一样的吊人。 The user can input a "secret sentence" and then the players have to guess it by chosing letters one by one.用户可以输入一个“秘密句子”,然后玩家必须通过一个一个选择的字母来猜测它。

Since this game will be used in several languages, I would like to solve all accented character if a player chose the non-accented character.由于此游戏将在多种语言中使用,如果玩家选择非重音字符,我想解决所有重音字符。 For example:例如:

IL CANE È BELLO.

If the player choses the vowel "e" only the vowels without accent will be solved (the game compares characters with ===).如果玩家选择元音“e”,则只会解析没有重音的元音(游戏用 === 比较字符)。 Instead I would like that the game recognizes automatically all chars accented or not (è, é, e...)相反,我希望游戏能自动识别所有带重音或不带重音的字符(è、é、e...)

I'm about to torture myself writing a function that checks every accented character, but I was wondering if ther might be an easier way.我要折磨自己写一个函数来检查每个带重音的字符,但我想知道是否有更简单的方法。

This is what my funciton look like so far (I just added the è accented vowel)这是我的函数到目前为止的样子(我刚刚添加了 è 重音元音)

function isAccentedVowel(chosenChar, currentChar) {
    console.log('The user selected this char: ' + chosenChar)
    console.log('I\'m comparing it to this char: ' + currentChar)
    switch (chosenChar) {
        case 'E':
            if (currentChar === 'È') return true;
    }
}

note: the secret sentence and every character are uppercase注意:密句和每个字符都是大写的

You'll want to use String.prototype.localeCompare() with sensitivity value of "base" .您需要使用sensitivity值为"base" 的String.prototype.localeCompare() This has the added bonus of comparing case-insensitively (which I assume you'll also want).这具有不区分大小写的比较的额外好处(我假设您也会想要)。

If your characters match, localeCompare() returns 0 .如果您的字符匹配,则localeCompare()返回0

Here's an example based on the assumption that you'll want to find the positions of the matching characters (ie, a "hang man" game)这是一个基于假设您要查找匹配字符的位置的示例(即“吊人”游戏)

 const locale = undefined // set this if it matters, otherwise use default const options = { sensitivity: 'base' } const findCharPositions = (str, char) => Array.prototype.reduce.call(str, (indexes, c, i) => (c.localeCompare(char, locale, options) || indexes.push(i), indexes), []) const str = 'IL CANE È BELLO' console.log('Find "l":', findCharPositions(str, 'l')) console.log('Find "e":', findCharPositions(str, 'e'))

You can use String#normalize to compare the accented characters with their normal counterpart.您可以使用String#normalize将重音字符与其正常对应物进行比较。

To perform comparison, you need to first get/extract normal character from accented character as below.要进行比较,您需要首先从重音字符中获取/提取正常字符,如下所示。

string.normalize('NFD')

This will return an array containing two items.这将返回一个包含两个项目的数组。 You can now compare the first item in the array with normal characters.您现在可以将数组中的第一项与普通字符进行比较。

 function isAccented(char, accentedChar) { return char === accentedChar.normalize('NFD')[0]; } console.log(isAccented('E', 'È'));

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM