Javascript + Unicode正则表达式 [英] Javascript + Unicode regexes

查看:250
本文介绍了Javascript + Unicode正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在JavaScript中使用支持Unicode的正则表达式?例如,应该有类似于\w的东西可以匹配字母或标记类别中的任何代码点(不仅仅是ASCII代码),并且希望像[[P *]]这样的过滤器用于标点符号等。

解决方案

ES 6的情况



即将推出的ECMAScript语言规范,第6版,包括支持Unicode的正则表达式。必须使用正则表达式上的 u 修饰符启用支持。请参阅 ES6中支持Unicode的正则表达式



在ES 6完成并在浏览器供应商中广泛采用之前,你仍然可以自己动手。 更新:现在有一个名为 regexpu 的转录程序ES6 Unicode正则表达式转换为等效的ES5。它可以用作构建过程的一部分。 在线试用。



ES 5和在



尽管JavaScript在Unicode字符串上运行,但它不实现支持Unicode的字符类,并且没有POSIX字符类或Unicode块/子范围的概念。 / p>


How can I use Unicode-aware regular expressions in JavaScript? For example, there should be something akin to \w that can match any code-point in Letters or Marks category (not just the ASCII ones), and hopefully have filters like [[P*]] for punctuation etc.

解决方案

Situation for ES 6

The upcoming ECMAScript language specification, edition 6, includes Unicode-aware regular expressions. Support must be enabled with the u modifier on the regex. See Unicode-aware regular expressions in ES6.

Until ES 6 is finished and widely adopted among browser vendors you're still on your own, though. Update: There is now a transpiler named regexpu that translates ES6 Unicode regular expressions into equivalent ES5. It can be used as part of your build process. Try it out online.

Situation for ES 5 and below

Even though JavaScript operates on Unicode strings, it does not implement Unicode-aware character classes and has no concept of POSIX character classes or Unicode blocks/sub-ranges.

这篇关于Javascript + Unicode正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆