使用特殊字符的JavaScript编码

时间:2012-11-06 09:42:01

标签: javascript encoding utf-8

我想编写一种方法来将特殊字符转换为响应的Unicode(例如\ u00e4)。

由于某种原因,JS发现在内部甚至没有保存'ä'但使用'ü'或其他一些乱码很有趣,所以当我转换它时会吐出'\ u00c3 \ u00c3 \ u00c3 \ u002013',因为它会转换这些chars而不是'ä'。

我已经尝试将HTML文件的编码设置为utf-8,并尝试使用charset =“UTF-8”加载脚本无济于事。代码并没有真正做任何特别的事情,但这里是:

String.prototype.replaceWithUtf8 = function() {
    var str_newString = '';
    var str_procString = this;

    for (var i = 0; i < str_procString.length; i++) {
        if (str_procString.charCodeAt(i) > 126) {
            var hex_uniCode = '\\u00' + str_procString.charCodeAt(i).toString(16);
            console.log(hex_uniCode + " (" + str_procString.charAt(i) + ")");
            str_newString += hex_uniCode;
        } else {
            str_newString += str_procString.charAt(i);
        }
    }
    return str_newString;
}
var str_item = "Lärm, Lichter, Lücken, Löcher."

console.log(str_item); // Lärm, Lichter, Lücken, Löcher. 
console.log(str_item.replaceWithUtf8()); //L\u00c3\u00a4rm, Lichter, L\u00c3\u00bccken, L\u00c3\u00b6cher. 

3 个答案:

答案 0 :(得分:2)

我不知道如何或为什么但我只是重新启动服务器,现在它正确显示。去跟随;这是所有感兴趣的人的代码:

String.prototype.replaceWithUtf8 = function() {
    var str_newString = '';
    var str_procString = this;
    var arr_replace = new Array('/', '"');
    var arr_replaceWith = new Array('\\/', '\\"');

    for (var i = 0; i < str_procString.length; i++) {
        var int_charCode = str_procString.charCodeAt(i);
        var cha_charAt = str_procString.charAt(i);
        var int_chrIndex = arr_replace.indexOf(cha_charAt);

        if (int_chrIndex > -1) {
            console.log(arr_replaceWith[int_chrIndex]);
            str_newString += arr_replaceWith[int_chrIndex];
        } else {
            if (int_charCode > 126 && int_charCode < 65536) {
                var hex_uniCode = '\\u' + ("000" + int_charCode.toString(16)).substr(-4);
                console.log(hex_uniCode + " (" + cha_charAt + ")");
                str_newString += hex_uniCode;
            } else {
                str_newString += cha_charAt;
            }
        }
    }
    return str_newString;
}

答案 1 :(得分:2)

使用'\\u' + ('000' + str_procString.charCodeAt(i).toString(16) ).stubstr(-4);来获取正确的转义序列 - 您的转义始终以00开头。此外,代替for循环处理您的字符串,.replace()可能会更快。

关于你的问题:

console.log("Lärm, Lichter, Lücken, Löcher."); // Lärm, Lichter, Lücken, Löcher.

没声音,因为您确实使用正确的编码发送了文件。如果已经正确保存,也可能是服务器问题。

答案 2 :(得分:0)

&#13;
&#13;
String.prototype.replaceWithUtf8 = function() {
  function r(r) {
    for (var t, n, e = "", i = 0; !isNaN(t = r.charCodeAt(i++)); ) n = t.toString(16), 
    e += 256 > t ? "\\x" + (t > 15 ? "" :"0") + n :"\\u" + ("0000" + n).slice(-4);
    return e;
  }
  var a, c, o, u, s, e = "", i = this, t = [ "/", '"' ], n = [ "\\/", '\\"' ];
  for (a = 0; a < i.length; a++) c = i.charCodeAt(a), o = i.charAt(a), u = t.indexOf(o), 
  u > -1 ? e += n[u] :c > 126 && 65536 > c ? (s = r(o), e += s) :e += o;
  return e;
};

prompt("Your escaped string:","Lärm, Lichter, Lücken, Löcher.".replaceWithUtf8());

alert("L\xe4rm, Lichter, L\xfccken, L\xf6cher.");
&#13;
&#13;
&#13;

Unicode编码仅使每个字符为6位数。但是对于127到256以上的字符,我们实际上可以用较少的字节(每个字符4位)来制作这些十六进制数。

相关问题