如果 utf 8 字符不是硬编码问题,Chrome 和 Safari 处理 utf 8 的方式与 Firefox 和 IE 不同?

发布于 2024-10-19 19:24:18 字数 3824 浏览 1 评论 0原文

问答中所述

注意:问题标题已更改,如本元 将 jQuery bassistance 自动完成插件与 口音插件 一起使用,所以我有口音-免费自动完成。重音映射是这样的:

map={'À':'A', 'İ':'I'};

我对字符 I (带有点的土耳其大写 I)有疑问。删除重音符号并转换为小写后,我得到以下代码:

   ("İstanbul").indexOf("is")

Firefox 和 IE 给出 0,但 Chrome 和 Safari 给出 -1

charCodeAt(0) 在所有浏览器中给出相同的结果。

看来 Chrome 和 Safari 对普通的 I 和不带重音的 I 的处理方式有所不同。

编辑

再次嗨,大约 5 个月后,我终于可以为我的问题添加更多来源。
幸运的是,问题日期和我在该日期的备份可以帮助我找到源代码。

当我用 AFTER (WORKS OK) 更改代码时,BEFORE(TURKISH © BUG) 已修复。
下面我还添加了我的文档字符集(土耳其语)

我不是 unicode 专家,但如果你让我猜测“chrome vs ff”问题:
chrome 使用“源代码”处理字符,我的意思是像 \u0128
ff 用“可见字符”处理它,我的意思是“?”

之前(土耳其语我的错误)

function remove_accent(str) {var map={'À':'A','Á':'A','Â':'A','Ã':'A','Ä':'A','Å':'A','Æ':'AE','Ç':'C','È':'E','É':'E','Ê':'E','Ë':'E','Ì':'I','Í':'I','Î':'I','Ï':'I','Ð':'D','Ñ':'N','Ò':'O','Ó':'O','Ô':'O','Õ':'O','Ö':'O','Ø':'O','Ù':'U','Ú':'U','Û':'U','Ü':'U','Ý':'Y','ß':'s','à':'a','á':'a','â':'a','ã':'a','ä':'a','å':'a','æ':'ae','ç':'c','è':'e','é':'e','ê':'e','ë':'e','ì':'i','í':'i','î':'i','ï':'i','ñ':'n','ò':'o','ó':'o','ô':'o','õ':'o','ö':'o','ø':'o','ù':'u','ú':'u','û':'u','ü':'u','ý':'y','ÿ':'y','Ā':'A','ā':'a','Ă':'A','ă':'a','Ą':'A','ą':'a','Ć':'C','ć':'c','Ĉ':'C','ĉ':'c','Ċ':'C','ċ':'c','Č':'C','č':'c','Ď':'D','ď':'d','Đ':'D','đ':'d','Ē':'E','ē':'e','Ĕ':'E','ĕ':'e','Ė':'E','ė':'e','Ę':'E','ę':'e','Ě':'E','ě':'e','Ĝ':'G','ĝ':'g','Ğ':'G','ğ':'g','Ġ':'G','ġ':'g','Ģ':'G','ģ':'g','Ĥ':'H','ĥ':'h','Ħ':'H','ħ':'h','Ĩ':'I','ĩ':'i','Ī':'I','ī':'i','Ĭ':'I','ĭ':'i','Į':'I','į':'i','İ':'I','ı':'i','IJ':'IJ','ij':'ij','Ĵ':'J','ĵ':'j','Ķ':'K','ķ':'k','Ĺ':'L','ĺ':'l','Ļ':'L','ļ':'l','Ľ':'L','ľ':'l','Ŀ':'L','ŀ':'l','Ł':'L','ł':'l','Ń':'N','ń':'n','Ņ':'N','ņ':'n','Ň':'N','ň':'n','ʼn':'n','Ō':'O','ō':'o','Ŏ':'O','ŏ':'o','Ő':'O','ő':'o','Œ':'OE','œ':'oe','Ŕ':'R','ŕ':'r','Ŗ':'R','ŗ':'r','Ř':'R','ř':'r','Ś':'S','ś':'s','Ŝ':'S','ŝ':'s','Ş':'S','ş':'s','Š':'S','š':'s','Ţ':'T','ţ':'t','Ť':'T','ť':'t','Ŧ':'T','ŧ':'t','Ũ':'U','ũ':'u','Ū':'U','ū':'u','Ŭ':'U','ŭ':'u','Ů':'U','ů':'u','Ű':'U','ű':'u','Ų':'U','ų':'u','Ŵ':'W','ŵ':'w','Ŷ':'Y','ŷ':'y','Ÿ':'Y','Ź':'Z','ź':'z','Ż':'Z','ż':'z','Ž':'Z','ž':'z','ſ':'s','ƒ':'f','Ơ':'O','ơ':'o','Ư':'U','ư':'u','Ǎ':'A','ǎ':'a','Ǐ':'I','ǐ':'i','Ǒ':'O','ǒ':'o','Ǔ':'U','ǔ':'u','Ǖ':'U','ǖ':'u','Ǘ':'U','ǘ':'u','Ǚ':'U','ǚ':'u','Ǜ':'U','ǜ':'u','Ǻ':'A','ǻ':'a','Ǽ':'AE','ǽ':'ae','Ǿ':'O','ǿ':'o'};var res='';for (var i=0;i<str.length;i++){c=str.charAt(i);res+=map[c]||c;}return res;}  

之后(工作正常)

var charToAccentedCharClassMap = {
// A to H
    'I': '[Iiı\xcc-\xcf\xec-\xef\u0128-\u0130\u0132\u0133\u01cf\u01d0\u0208-\u020b\u1d35\u1d62\u1e2c\u1e2d\u1ec8-\u1ecb\u2071\u2110\u2111\u2139\u2148\u2160-\u2163\u2165-\u2168\u216a\u216b\u2170-\u2173\u2175-\u2178\u217a\u217b\u24a4\u24be\u24d8\u337a\u33cc\u33d5\ufb01\ufb03\uff29\uff49]'
// J to Z
    };

函数 deaccent(accentedString) { var 结果 = 重音字符串; for (charToAccentedCharClassMap 中的 var key) { 结果 = result.replace(new RegExp(charToAccentedCharClassMap[key], "g"), key); } //console.log(accentedString) 返回结果.toLowerCase(); }

函数remove_accent(str) { 返回 deaccent(str).toLowerCase(); 返回字符串; }

EDIT2 小修正(所以用户编辑了问题,但错过了他的键盘):
他将 ("伊斯坦布尔").indexOf("is") 替换为 ("istanbul").indexOf("is")

note: question title is change as discussed in this meta Q&A

I'm using the jQuery bassistance autocomplete plugin with the accent plugin, so I have accent-free autocomplete. Accent map is like this:

map={'À':'A', 'İ':'I'};

I have problems with the character İ (turkish uppercase I with point). After I remove accents and convert to lowercase, I have this code:

   ("İstanbul").indexOf("is")

Firefox and IE gives 0, but Chrome and Safari gives -1.

charCodeAt(0) gives the same result in all browsers.

It seems that Chrome and Safari treats the normal I and the unaccented I different.

EDIT

Hi again, after about 5 months, I can finally add more source to my question.
Fortunately question date and my backups at that date helps me find source code.

BEFORE(TURKISH İ BUG) was fixed when I change my code with AFTER (WORKS OK).
Below I added also my document charshet(which is turkish)

I am not unicode expert, but if you let me guess about the "chrome vs ff" issue:
chrome handles the characther with "source code", I mean like \u0128
ff handles it with "visible characher", I mean like "İ"

BEFORE(TURKISH I BUG)

function remove_accent(str) {var map={'À':'A','Á':'A','Â':'A','Ã':'A','Ä':'A','Å':'A','Æ':'AE','Ç':'C','È':'E','É':'E','Ê':'E','Ë':'E','Ì':'I','Í':'I','Î':'I','Ï':'I','Ð':'D','Ñ':'N','Ò':'O','Ó':'O','Ô':'O','Õ':'O','Ö':'O','Ø':'O','Ù':'U','Ú':'U','Û':'U','Ü':'U','Ý':'Y','ß':'s','à':'a','á':'a','â':'a','ã':'a','ä':'a','å':'a','æ':'ae','ç':'c','è':'e','é':'e','ê':'e','ë':'e','ì':'i','í':'i','î':'i','ï':'i','ñ':'n','ò':'o','ó':'o','ô':'o','õ':'o','ö':'o','ø':'o','ù':'u','ú':'u','û':'u','ü':'u','ý':'y','ÿ':'y','Ā':'A','ā':'a','Ă':'A','ă':'a','Ą':'A','ą':'a','Ć':'C','ć':'c','Ĉ':'C','ĉ':'c','Ċ':'C','ċ':'c','Č':'C','č':'c','Ď':'D','ď':'d','Đ':'D','đ':'d','Ē':'E','ē':'e','Ĕ':'E','ĕ':'e','Ė':'E','ė':'e','Ę':'E','ę':'e','Ě':'E','ě':'e','Ĝ':'G','ĝ':'g','Ğ':'G','ğ':'g','Ġ':'G','ġ':'g','Ģ':'G','ģ':'g','Ĥ':'H','ĥ':'h','Ħ':'H','ħ':'h','Ĩ':'I','ĩ':'i','Ī':'I','ī':'i','Ĭ':'I','ĭ':'i','Į':'I','į':'i','İ':'I','ı':'i','IJ':'IJ','ij':'ij','Ĵ':'J','ĵ':'j','Ķ':'K','ķ':'k','Ĺ':'L','ĺ':'l','Ļ':'L','ļ':'l','Ľ':'L','ľ':'l','Ŀ':'L','ŀ':'l','Ł':'L','ł':'l','Ń':'N','ń':'n','Ņ':'N','ņ':'n','Ň':'N','ň':'n','ʼn':'n','Ō':'O','ō':'o','Ŏ':'O','ŏ':'o','Ő':'O','ő':'o','Œ':'OE','œ':'oe','Ŕ':'R','ŕ':'r','Ŗ':'R','ŗ':'r','Ř':'R','ř':'r','Ś':'S','ś':'s','Ŝ':'S','ŝ':'s','Ş':'S','ş':'s','Š':'S','š':'s','Ţ':'T','ţ':'t','Ť':'T','ť':'t','Ŧ':'T','ŧ':'t','Ũ':'U','ũ':'u','Ū':'U','ū':'u','Ŭ':'U','ŭ':'u','Ů':'U','ů':'u','Ű':'U','ű':'u','Ų':'U','ų':'u','Ŵ':'W','ŵ':'w','Ŷ':'Y','ŷ':'y','Ÿ':'Y','Ź':'Z','ź':'z','Ż':'Z','ż':'z','Ž':'Z','ž':'z','ſ':'s','ƒ':'f','Ơ':'O','ơ':'o','Ư':'U','ư':'u','Ǎ':'A','ǎ':'a','Ǐ':'I','ǐ':'i','Ǒ':'O','ǒ':'o','Ǔ':'U','ǔ':'u','Ǖ':'U','ǖ':'u','Ǘ':'U','ǘ':'u','Ǚ':'U','ǚ':'u','Ǜ':'U','ǜ':'u','Ǻ':'A','ǻ':'a','Ǽ':'AE','ǽ':'ae','Ǿ':'O','ǿ':'o'};var res='';for (var i=0;i<str.length;i++){c=str.charAt(i);res+=map[c]||c;}return res;}  

AFTER (WORKS OK)

var charToAccentedCharClassMap = {
// A to H
    'I': '[Iiı\xcc-\xcf\xec-\xef\u0128-\u0130\u0132\u0133\u01cf\u01d0\u0208-\u020b\u1d35\u1d62\u1e2c\u1e2d\u1ec8-\u1ecb\u2071\u2110\u2111\u2139\u2148\u2160-\u2163\u2165-\u2168\u216a\u216b\u2170-\u2173\u2175-\u2178\u217a\u217b\u24a4\u24be\u24d8\u337a\u33cc\u33d5\ufb01\ufb03\uff29\uff49]'
// J to Z
    };


function deaccent(accentedString) {
var result = accentedString;
for (var key in charToAccentedCharClassMap) {
result = result.replace(new RegExp(charToAccentedCharClassMap[key], "g"), key);
}
//console.log(accentedString)
return result.toLowerCase();
}

function remove_accent(str) {
return deaccent(str).toLowerCase();
return str;
}

EDIT2 little correction (SO user edited question but misses İ his keyboard):
He replaces ("İstanbul").indexOf("is") by ("istanbul").indexOf("is")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不吻晚风 2024-10-26 19:24:18
("istanbul").indexOf("is")
("Istanbul").toLowerCase().indexOf("ıs")

小写的“I”是“ı”。这就是这里发生的事情吗?

编辑:

可能为时已晚,但我现在记得我遇到了一个非常相似的情况,其中“@”与“@”不同,因为它是不同的unicode字符。我通过硬编码字符串中的 Unicode 数字解决了这个问题,如“\u0040

("istanbul").indexOf("is")
("Istanbul").toLowerCase().indexOf("ıs")

Lowercase 'I' is 'ı'. Is that what is happening here?

EDIT:

Probably too late, but I remember now I encountered a very similar case where "@" was different from "@" because it was a different unicode character. I solved it by hardcoding the Unicode number in the string like "\u0040"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文