使用JavaScript执行带/不带重音字符的文本匹配 [英] Using JavaScript to perform text matches with/without accented characters

查看:142
本文介绍了使用JavaScript执行带/不带重音字符的文本匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用基于AJAX的查找来查找用户在文本框中搜索的名称。

I am using an AJAX-based lookup for names that a user searches in a text box.

我假设数据库中的所有名称都是音译为欧洲字母(即没有西里尔字母,日文,中文)。但是,名称仍然包含重音字符,例如ç,ê甚至č和ć。

I am making the assumption that all names in the database will be transliterated to European alphabets (i.e. no Cyrillic, Japanese, Chinese). However, the names will still contain accented characters, such as ç, ê and even č and ć.

像Micic这样的简单搜索与Mičić不匹配 - 并且用户期望它会。

A simple search like "Micic" will not match "Mičić" though - and the user expectation is that it will.

AJAX查找使用正则表达式来确定匹配。我已使用此函数修改了正则表达式比较,以尝试匹配更多重音字符。但是,它有点笨拙,因为它没有考虑所有字符。

The AJAX lookup uses regular expressions to determine a match. I have modified the regular expression comparison using this function in an attempt to match more accented characters. However, it's a little clumsy since it doesn't take into account all characters.

function makeComp (input)
{
    input = input.toLowerCase ();
    var output = '';
    for (var i = 0; i < input.length; i ++)
    {
        if (input.charAt (i) == 'a')
            output = output + '[aàáâãäåæ]'
        else if (input.charAt (i) == 'c')
            output = output + '[cç]';
        else if (input.charAt (i) == 'e')
            output = output + '[eèéêëæ]';
        else if (input.charAt (i) == 'i')
            output = output + '[iìíîï]';
        else if (input.charAt (i) == 'n')
            output = output + '[nñ]';
        else if (input.charAt (i) == 'o')
            output = output + '[oòóôõöø]';
        else if (input.charAt (i) == 's')
            output = output + '[sß]';
        else if (input.charAt (i) == 'u')
            output = output + '[uùúûü]';
        else if (input.charAt (i) == 'y')
            output = output + '[yÿ]'
        else
            output = output + input.charAt (i);
    }
    return output;
}

除了这样的替换功能外,还有更好的方法吗?也许是为了去除被比较的字符串?

Apart from a substitution function like this, is there a better way? Perhaps to "deaccent" the string being compared?

推荐答案

这应该有所帮助:它被称为重音折叠:

this should help: its called accent folding:

http://alistapart.com/article / accent-folding-for-auto-complete

这篇关于使用JavaScript执行带/不带重音字符的文本匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆