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 ?.
A simple search like "Micic" will not match "Mi?i?" though - and the user expectation is that it will.
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àá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?
See Question&Answers more detail:os