正则表达式字面量在ECMAScript5中的变化

作者:秦歌 时间:2012-04-26 16:23:16 


在《JavaScript语言精粹》的第72页有这样一段:

用正则表达式字面量创建的RegExp对象来共享同一个单实例:function make_a_matcher(  ) {    return /a/gi;}var x = make_a_matcher(  );var y = make_a_matcher(  );// 注意:x 和 y 是同一个对象!x.lastIndex = 10;document.writeln(y.lastIndex);    // 10

当你在浏览器中运行这段代码时,你会发现IE6-IE9、FireFox4、Chrome10、Safari5输出都是0,Firefox 3.6.16输出是10,原因可以在ECMAScript5规范第24页和第247页找到:


A regular expression literal is an input element that is converted to a RegExp object (see 15.10) each time the literal is evaluated. Two regular expression literals in a program evaluate to regular expression objects that never compare as === to each other even if the two literals’ contents are identical. A RegExp object may also be created at runtime by new RegExp (see 15.10.4) or calling the RegExp constructor as a function (15.10.3).

7.8.5: Regular expression literals now return a unique object each time the literal is evaluated. This change is detectable by any programs that test the object identity of such literal values or that are sensitive to the shared side effects.

也就是说在ECMAScript3规范中,用正则表达式创建的RegExp对象会共享同一个实例,而在ECMAScript5中则是两个独立的实例。《JavaScript语言精粹》出版时ECMAScript5还没有发布,在这个问题上书和ECMAScript3标准保持了一致。FireFox3.6遵循了ECMAScript3标准,所以结果与书中一致,而最新的Firefox4、Chrome和Safari5都遵循ECMAScript5标准,至于IE6-IE8都没有很好的遵循ECMAScript3标准,不过在这个问题上反而处理对了。很明显ECMAScript5的规范更符合开发者的期望,那就是相同的正则表达式字面量创建独立的RegExp对象会有不同的lastIndex,才方便分别处理。

在ECMAScript5规范的第247页还有两条来说明ECMAScript5和ECMAScript3在正则表达式字面量上的改变:

7.8.5: Edition 5 requires early reporting of any possible RegExp constructor errors that would be produced when converting a RegularExpressionLiteral to a RegExp object. Prior to Edition 5 implementations were permitted to defer the reporting of such errors until the actual execution time creation of the object.
7.8.5: In Edition 5 unescaped “/” characters may appear as a CharacterClass in a regular expression literal. In Edition 3 such a character would have been interpreted as the final character of the literal.

第1个是在ECMAScript5中正则表达式字面量转化为RegExp对象时,任何RegExp构造器的错误都会尽早报告,而在之前的规范中是只有对象创建真正执行时才会报错。

第2个是说在ECMAScript5的正则表达式字面量中,未转义的正斜杠“/”可以直接用在正则表达式字符类中。而在ECMAScript3中它只能作为正则表达式字面量的开始和结束字符。从IE6-IE9、Firefox3.6-Firefox4.0、Chrome和Safari都可以直接把未转义的正斜杠“/”用在正则表达式字符类中。如:

var my_regexp = /([8/5+4]*).{3}/g;var str = '8/5+4 is what!';var result = my_regexp.exec(str); // the same in IE6-9,FF3.6-4.0,Chrome,Safarifor(var i = 0,n = result.length; i < n; ++i){  document.writeln(result[i]);}result[0] = 8/5+4 isresult[1] = 8/5+4

在《JavaScript语言精粹》第76页也指明在正则表达式的字符类中使用正斜杠“/”需要转义,也是基于ECMAScript3规范。由于正则表达式中需要转义的特殊字符比较多,当心存疑虑时对任何特殊字符都可以使用反斜杠“\”来使其字面化确保安全,不过这个规则不适宜字母和数字。

正则表达式字面量从ECMAScript3到ECMAScript5的改变也蛮符合HTML5设计原理中提到的2条。一条是“一旦遇到冲突,最终用户优先,其次是作者,其次是实现者,其次标准制定者,最后才是理论上的完满”,另一条是“支持已有内容”。

标签:正则表达式,ECMAScript5
0
投稿

猜你喜欢

  • Microsoft SQL Server数据库SA权限总结

    2009-01-06 11:32:00
  • MySQL复制的概述、安装、故障、技巧、工具

    2011-04-11 08:36:00
  • W3C优质网页小贴士(四)

    2008-04-17 13:34:00
  • 必须知道的10个不常用HTML标签[译]

    2009-03-31 13:19:00
  • Dreamweaver MX 2004新特点

    2008-02-03 11:35:00
  • 安装PHP遇到“无法载入mysql扩展”解决方法

    2007-06-15 15:04:00
  • position两三事

    2009-02-16 15:23:00
  • js打开新窗口方法代码收集

    2007-09-05 19:20:00
  • asp实现页面延迟运行的两个简单方法

    2007-10-16 13:49:00
  • div中class与id的区别及应用

    2007-09-22 08:37:00
  • IE中选择符的4095限制

    2009-10-09 13:25:00
  • 判断触发器正在处理的是插入,删除还是更新触发

    2012-01-29 18:30:34
  • MySQL查询优化

    2009-03-09 14:41:00
  • 寻找Dreamweaver鲜为人知的小秘诀

    2008-04-28 12:10:00
  • ASP如何跳出本次进入下一次循环

    2008-10-23 13:46:00
  • MYSQL数据库设计的一点总结

    2008-05-24 09:36:00
  • iframe框架用JavaScript子页面控制父页面

    2009-01-19 13:43:00
  • 透明数据加密(TDE)库的备份和还原

    2012-07-21 14:44:08
  • sql2005与sql200数据导入导出

    2009-03-13 13:16:00
  • javascript阻止事件冒泡和浏览器的默认行为

    2007-12-28 13:13:00
  • asp之家 网络编程 m.aspxhome.com