JavaScript Unicode normalization

老子叫甜甜 提交于 2019-11-29 03:34:34
bobince

No, there is no Unicode Normalization feature used automatically on—or even available to—JavaScript as per ECMAScript 5. All characters remain unchanged as their original code points, potentially in a non-Normal Form.

eg try:

<script type="text/javascript">
    var a= 'café';          // caf\u00E9
    var b= 'café';          // cafe\u0301
    alert(a+' '+a.length);  // café 4
    alert(b+' '+b.length);  // café 5
    alert(a==b);            // false
</script>

Update: ECMAScript 6 will introduce Unicode normalization for JavaScript strings.

ECMAScript 6 introduces String.prototype.normalize() which takes care of Unicode normalization for you.

unorm is a JavaScript polyfill for this method, so that you can already use String.prototype.normalize() today even though not a single engine supports it natively at the moment.

For more information on how and when to use Unicode normalization in JavaScript, see JavaScript has a Unicode problem – Accounting for lookalikes.

If you're using node.js, there is a unorm library for this.

https://github.com/walling/unorm

I've updated @bobince 's answer:

var cafe4= 'caf\u00E9';
var cafe5= 'cafe\u0301';


console.log (
  cafe4+' '+cafe4.length,                  // café 4
  cafe5+' '+cafe5.length,                  // café 5
  cafe4 === cafe5,                         // false
  cafe4.normalize() === cafe5.normalize()  // true
);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!