Javascript charAt()打破多字节字符串

时间:2013-11-03 12:33:27

标签: javascript node.js multibyte encodeuricomponent charat

此代码符合nodejs v0.10.21

#!/usr/bin/env node
"use strict";

var urlEncoded = 'http://zh.wikipedia.org/wiki/%F0%A8%A8%8F';
var urlDecoded = decodeURI( urlEncoded );
var urlLeafEncoded = urlEncoded.substr( 29 );
var urlLeafDecoded = decodeURIComponent( urlLeafEncoded );
var urlLeafFirstCharacterDecoded = urlLeafDecoded.charAt( 0 );
var urlLeafFirstCharacterEncoded = encodeURIComponent( urlLeafFirstCharacterDecoded );

console.log( 'URL encoded = ' + urlEncoded );
console.log( 'URL decoded = ' + urlDecoded );
console.log( 'URL leaf encoded = ' + urlLeafEncoded );
console.log( 'URL leaf decoded = ' + urlLeafDecoded );
console.log( 'URL leaf first character encoded = ' + urlLeafEncoded );
console.log( 'URL leaf first character decoded = ' + urlLeafDecoded );

我收到以下错误

var urlLeafFirstCharacterEncoded = encodeURIComponent( urlLeafFirstCharacterDe
                               ^
URIError: URI malformed
    at encodeURIComponent (native)
    at Object.<anonymous> (/media/data/tmp/mwoffliner/test.js:9:36)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

Javascript用于正确处理多字节字符,但不是那种情况。似乎虽然“%F0%A8%A8%8F”代表一个中文字符,但javascript认为它们是两个中文字符。我很困惑,如果这是javascript运行时的错误,不知何故是一个编码问题,或者在我身边被误解。

1 个答案:

答案 0 :(得分:3)

位于BMP之外,由于Javascript仅使用2个字节来存储字符,因此表示为surrogate pair。虽然encodeURIComponent可以对代理对进行操作并为它们生成正确的UTF8编码,但它无法单独读取代理。因此,encodeURIComponent("")工作正常,encodeURIComponent("".charAt(0))将失败。

有关详细信息,请参阅http://mathiasbynens.be/notes/javascript-encoding。此外,https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent专门记录了此用例。