问题描述
我有此代码:
if (term.length > 0) {
var inputVal = $("#input").val();
if (inputVal.indexOf("jason") !== -1) {
results = $.ui.autocomplete.filter(table, term);
}
}
这有效.但是,当我将"jason"
更改为jasön"
或jasün"
或其他土耳其语字符时,它不起作用.是因为js文件的编码吗?我将编码更改为Unicode(带签名的utf-8)-代码页65001,然后是土耳其语ISO 28599,但没有用.你能告诉我该怎么办吗?谢谢.
This works. But when I changed "jason"
to "jasön"
or "jasün"
or something else which is a Turkish character, it doesn't work. Is it because the encoding of the js file? I changed the encoding to Unicode (utf-8 with signature) - Codepage 65001 then Turkish ISO 28599 but it didn't work. Can you tell me what I should do? Thanks.
推荐答案
它可以识别土耳其语字符,但是您正在对所述字符串进行相等性检查.您需要了解的是,即使ö
只是带有重音符号的 o
,它仍然是一个不同字符,因此使您的条件错误,即使您没有进行三重相等检查.
It does recognize Turkish characters, but you are doing an equality check on said string. What you have to understand is that even if ö
is only an o
with an accent, it's still a different character, thus making your conditional falsy, even if you are not doing a triple equality check.
'ö' == 'o' // false
'ö' === 'o' // false
您应该做的是将输入值转换为没有重音符号的字符串,因为这显然是您所期望的.并且这个问题正是您要寻找的,我会说将是最好的答案,因为它非常干净且易于使用
What you should do instead is convert the input value into a string without accents, since it's apparently what you were expecting. And this question is exactly what you are looking for, and I would say this answer would be the best one if you have access to ES6 features since it's pretty clean and simple to use
function convert (str) {
return str.normalize('NFD').replace(/[\u0300-\u036f]/g, '')
}
if (term.length > 0) {
var inputVal = convert($('#input').val())
if (inputVal.indexOf('jason') !== -1) {
results = $.ui.autocomplete.filter(table, term)
}
}
否则,如果没有ES6选项,则最佳答案应该可以使用,只需创建一个函数util您可以在需要的任何地方重复使用.
Otherwise if ES6 is not an option, the top answer should be fine to use, just create a function util that you can reuse anywhere you need to.
关于文件的编码,如果要处理土耳其语确实具有的特殊字符,则应使用 utf-8
.
Regarding the encoding of the file, you should use utf-8
if dealing with special chars that Turkish does have.
这篇关于JavaScript无法识别土耳其语字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!