在《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,Safari
for(var i = 0,n = result.length; i < n; ++i){
document.writeln(result[i]);
}
result[0] = 8/5+4 is
result[1] = 8/5+4
在《JavaScript语言精粹》第76页也指明在正则表达式的字符类中使用正斜杠“/”需要转义,也是基于ECMAScript3规范。由于正则表达式中需要转义的特殊字符比较多,当心存疑虑时对任何特殊字符都可以使用反斜杠“\”来使其字面化确保安全,不过这个规则不适宜字母和数字。
正则表达式字面量从ECMAScript3到ECMAScript5的改变也蛮符合HTML5设计原理中提到的2条。一条是“一旦遇到冲突,最终用户优先,其次是作者,其次是实现者,其次标准制定者,最后才是理论上的完满”,另一条是“支持已有内容”。
最后推荐一下XRegExp,它是一个非常优秀的正则表达式JavaScript库,兼容多个主流浏览器、ECMAScript3和ECMAScript5。
用正则表达式字面量创建的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,Safari
for(var i = 0,n = result.length; i < n; ++i){
document.writeln(result[i]);
}
result[0] = 8/5+4 is
result[1] = 8/5+4
在《JavaScript语言精粹》第76页也指明在正则表达式的字符类中使用正斜杠“/”需要转义,也是基于ECMAScript3规范。由于正则表达式中需要转义的特殊字符比较多,当心存疑虑时对任何特殊字符都可以使用反斜杠“\”来使其字面化确保安全,不过这个规则不适宜字母和数字。
正则表达式字面量从ECMAScript3到ECMAScript5的改变也蛮符合HTML5设计原理中提到的2条。一条是“一旦遇到冲突,最终用户优先,其次是作者,其次是实现者,其次标准制定者,最后才是理论上的完满”,另一条是“支持已有内容”。
最后推荐一下XRegExp,它是一个非常优秀的正则表达式JavaScript库,兼容多个主流浏览器、ECMAScript3和ECMAScript5。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年09月21日
2024年09月21日
- 群星.1995-烧得厉害VOL.4两个世界【滚石】【WAV+CUE】
- 归龙潮中浦城区宝箱位置收集大全 中浦区宝箱解密攻略
- 黑神话悟空上品幽灯鬼精魄获取方法一览|上品幽灯鬼精魄收集攻略
- 《电锯糖心:RePOP》发售首日表现亮眼,玩家峰值超越《星鸣特攻》
- NewViennaOctet,ViennaWindSoloists-TheDeccaRecordings(2024)18CD[FLAC]-8
- 金梅子-《靓声妹3又见炊烟》[WAV+CUE]
- 杨阳《约定HQCDII》头版限量编号2024[WAV+CUE]
- 曝《黑神话:悟空》DLC明年推出 农历新年前后
- PS5日本涨价19% 索尼股价应声上涨2.7%
- 《黑神话》发行平台遭大规模网攻 外媒却鲜有报道
- 伍佰《爱你伍佰年 世纪典藏原音精选3CD》[WAV+CUE][2.3GB]
- 伍佰《爱情的尽头》[WAV+CUE][2.6GB]
- 伍佰 《光环摇滚之声DSD》[WAV+CUE][500MB]
- 王菲.1999-只爱陌生人【EMI百代】【WAV+CUE】
- 林美音.1993-酒后的心声【瑞华】【WAV+CUE】