Regular expression literal change in ECMAScript5 _ regular expression

Source: Internet
Author: User
On page 72nd of the JavaScript language essence, there is a paragraph:

RegExp object created with regular expression literal to share the same single instance:
Copy Code code as follows:

function Make_a_matcher () {
Return/a/gi;
}
var x = Make_a_matcher ();
var y = Make_a_matcher ();

Note: X and Y are the same object!

X.lastindex = 10;

Document.writeln (Y.lastindex); 10 When you run this code in the browser, you will find that IE6-IE9, FireFox4, Chrome10, Safari5 output are 0,firefox 3.6.16 output is 10, The reason can be found on page 24th and No. 247 of the ECMASCRIPT5 specification:


A Regular expression literal is a INPUT element ' is ' converted to a RegExp object (?? 15.10) evaluated. Two regular expression literals in a program evaluate to regular expression the objects that never compare as = = to each othe R even if the two literals ' contents are identical. A RegExp object may also is created at runtime by new RegExp (the = 15.10.4) or calling the RegExp as a function (15.10.3).

7.8.5:regular expression literals now return a unique object of the literal is evaluated. This are detectable by any programs so test the object identity of such literal values or that are sensitive to th E Shared side effects.

That is, in the ECMASCRIPT3 specification, RegExp objects created with regular expressions share the same instance, while in ECMAScript5 there are two separate instances. ECMASCRIPT5 has not yet been published when the "pristine language of JavaScript" was published, and the issue of the book was consistent with the ECMASCRIPT3 standard. FireFox3.6 followed the ECMASCRIPT3 standard, so the results were consistent with the book, and the newest Firefox4, Chrome and SAFARI5 followed the ECMASCRIPT5 standard, As for IE6-IE8 is not very good to follow the ECMASCRIPT3 standard, but on this issue instead of dealing with the right. It is obvious that the ECMASCRIPT5 specification is more in line with the developer's expectation that the same regular expression literal creates separate regexp objects that have different lastindex that can be easily handled separately.

on page No. 247 of the ECMASCRIPT5 specification, there are two lines to illustrate the change in the literal volume of the regular expression of ECMAScript5 and ECMASCRIPT3:

7.8.5:edition 5 requires early reporting of any possible RegExp constructor errors that would is 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 CR Eation of the object.
7.8.5:in Edition 5 unescaped "/" characters may appear as a characterclass in a regular expression. In Edition 3 Such a character would have been interpreted as the final character of the literal.

The 1th one is that when ECMAScript5 the expression literal to a RegExp object, any errors of the RegExp constructor are reported as early as possible, whereas in the previous specification only the object creation was actually executed.

The 2nd one is that in the ECMASCRIPT5 regular expression literal, the not escaped forward slash "/" can be used directly in the regular expression character class. In ECMASCRIPT3, it can only be the start and end character of a regular expression literal. From IE6-IE9, firefox3.6-firefox4.0, Chrome, and Safari, you can use the not-escaped forward slash "/" in the regular expression character class directly. Such as:
Copy Code code as follows:

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

Page 76th of the JavaScript language highlights also indicates that the use of a forward slash "/" in the character class of a regular expression needs to be escaped and is based on the ECMASCRIPT3 specification. Because there are more special characters to be escaped in a regular expression, it is possible to use a backslash "\" for any particular character when you are skeptical, but this rule is not appropriate for letters and numbers.

The change of regular expression literal from ECMASCRIPT3 to ECMAScript5 is also quite consistent with the 2 mentioned in the HTML5 design principle. One is "in the event of conflict, the end user first, followed by the author, followed by the implementation, followed by the standard-makers, the final is theoretical perfection", the other is "support existing content."

Finally, recommend Xregexp, a very good regular expression JavaScript library, compatible with a number of mainstream browsers, ECMASCRIPT3, and ECMAScript5.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.