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