在《JavaScript語(yǔ)言精粹》的第72頁(yè)有這樣一段:
用正則表達(dá)式字面量創(chuàng)建的RegExp對(duì)象來(lái)共享同一個(gè)單實(shí)例:
復(fù)制代碼 代碼如下:
function make_a_matcher( ) {
return /a/gi;
}
var x = make_a_matcher( );
var y = make_a_matcher( );
// 注意:x 和 y 是同一個(gè)對(duì)象!
x.lastIndex = 10;
document.writeln(y.lastIndex); // 10當(dāng)你在瀏覽器中運(yùn)行這段代碼時(shí),你會(huì)發(fā)現(xiàn)IE6-IE9、FireFox4、Chrome10、Safari5輸出都是0,F(xiàn)irefox 3.6.16輸出是10,原因可以在ECMAScript5規(guī)范第24頁(yè)和第247頁(yè)找到:
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.
也就是說(shuō)在ECMAScript3規(guī)范中,用正則表達(dá)式創(chuàng)建的RegExp對(duì)象會(huì)共享同一個(gè)實(shí)例,而在ECMAScript5中則是兩個(gè)獨(dú)立的實(shí)例?!禞avaScript語(yǔ)言精粹》出版時(shí)ECMAScript5還沒(méi)有發(fā)布,在這個(gè)問(wèn)題上書和ECMAScript3標(biāo)準(zhǔn)保持了一致。FireFox3.6遵循了ECMAScript3標(biāo)準(zhǔn),所以結(jié)果與書中一致,而最新的Firefox4、Chrome和Safari5都遵循ECMAScript5標(biāo)準(zhǔn),至于IE6-IE8都沒(méi)有很好的遵循ECMAScript3標(biāo)準(zhǔn),不過(guò)在這個(gè)問(wèn)題上反而處理對(duì)了。很明顯ECMAScript5的規(guī)范更符合開發(fā)者的期望,那就是相同的正則表達(dá)式字面量創(chuàng)建獨(dú)立的RegExp對(duì)象會(huì)有不同的lastIndex,才方便分別處理。
在ECMAScript5規(guī)范的第247頁(yè)還有兩條來(lái)說(shuō)明ECMAScript5和ECMAScript3在正則表達(dá)式字面量上的改變:
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個(gè)是在ECMAScript5中正則表達(dá)式字面量轉(zhuǎn)化為RegExp對(duì)象時(shí),任何RegExp構(gòu)造器的錯(cuò)誤都會(huì)盡早報(bào)告,而在之前的規(guī)范中是只有對(duì)象創(chuàng)建真正執(zhí)行時(shí)才會(huì)報(bào)錯(cuò)。
第2個(gè)是說(shuō)在ECMAScript5的正則表達(dá)式字面量中,未轉(zhuǎn)義的正斜杠“/”可以直接用在正則表達(dá)式字符類中。而在ECMAScript3中它只能作為正則表達(dá)式字面量的開始和結(jié)束字符。從IE6-IE9、Firefox3.6-Firefox4.0、Chrome和Safari都可以直接把未轉(zhuǎn)義的正斜杠“/”用在正則表達(dá)式字符類中。如:
復(fù)制代碼 代碼如下:
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語(yǔ)言精粹》第76頁(yè)也指明在正則表達(dá)式的字符類中使用正斜杠“/”需要轉(zhuǎn)義,也是基于ECMAScript3規(guī)范。由于正則表達(dá)式中需要轉(zhuǎn)義的特殊字符比較多,當(dāng)心存疑慮時(shí)對(duì)任何特殊字符都可以使用反斜杠“\”來(lái)使其字面化確保安全,不過(guò)這個(gè)規(guī)則不適宜字母和數(shù)字。
正則表達(dá)式字面量從ECMAScript3到ECMAScript5的改變也蠻符合HTML5設(shè)計(jì)原理中提到的2條。一條是“一旦遇到?jīng)_突,最終用戶優(yōu)先,其次是作者,其次是實(shí)現(xiàn)者,其次標(biāo)準(zhǔn)制定者,最后才是理論上的完滿”,另一條是“支持已有內(nèi)容”。
最后推薦一下XRegExp,它是一個(gè)非常優(yōu)秀的正則表達(dá)式JavaScript庫(kù),兼容多個(gè)主流瀏覽器、ECMAScript3和ECMAScript5。
您可能感興趣的文章:- ECMAScript6塊級(jí)作用域及新變量聲明(let)
- 在NodeJS中啟用ECMAScript 6小結(jié)(windos以及Linux)
- ECMAScript6的新特性箭頭函數(shù)(Arrow Function)詳細(xì)介紹
- ECMAScript 創(chuàng)建自己的js類庫(kù)
- JavaScript高級(jí)程序設(shè)計(jì)(第3版)學(xué)習(xí)筆記13 ECMAScript5新特性
- ECMAScript6變量的解構(gòu)賦值實(shí)例詳解