JavaScript Unicode 规范化
我的印象是 JavaScript 解释器假设它正在解释的源代码已经标准化。归一化究竟是做什么的?它不能是文本编辑器,否则源的明文表示将会改变。是否有一些“预处理器”可以进行标准化?
I'm under the impression that JavaScript interpreter assumes that the source code it is interpreting has already been normalized. What, exactly does the normalizing? It can't be the text editor, otherwise the plaintext representation of the source would change. Is there some "preprocessor" that does the normalization?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ECMAScript 6 引入了
String.prototype.normalize()
来为您处理 Unicode 规范化。unorm 是此方法的 JavaScript 填充,因此您已经可以使用
String.prototype.normalize()
今天,尽管目前没有一个引擎本身支持它。有关如何以及何时在 JavaScript 中使用 Unicode 规范化的详细信息,请参阅 JavaScript 有一个Unicode 问题 – 相似字符的计算。
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.
不,根据 ECMAScript 5,JavaScript 上没有自动使用(甚至可用)的 Unicode 规范化功能。所有字符都保持其原始代码点不变,可能采用非规范形式。
例如尝试:
更新: ECMAScript 6 将为 JavaScript 字符串引入 Unicode 规范化。
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:
Update: ECMAScript 6 will introduce Unicode normalization for JavaScript strings.
如果您使用的是
node.js
,则有一个unorm
库可以实现此目的。https://github.com/walling/unorm
If you're using
node.js
, there is aunorm
library for this.https://github.com/walling/unorm
我更新了@bobince 的答案:
I've updated @bobince 's answer: