Changes in the regular expression literal volume in ECMAScript5

Source: Internet
Author: User

On the 72nd page of JavaScript:

Use a regular expression to literally create a RegExp object to share the same single instance:Copy codeThe Code is 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 a browser, you will find that the IE6-IE9, FireFox4, Chrome10, Safari5 output are all 0, Firefox 3.6.16 output is 10, the cause can be found on the ECMAScript5 specifications page 24th and page 247th:

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.

In other words, in the ECMAScript3 specification, RegExp objects created using regular expressions will share the same instance, while ECMAScript5 is two independent instances. ECMAScript5 has not yet been released at the time of the publication of JavaScript language essence. The book on this issue is consistent with the ECMAScript3 standard. FireFox3.6 follows the ECMAScript3 standard, so the results are consistent with the book, while the latest Firefox4, Chrome and Safari5 all follow the ECMAScript5 standard, as for IE6-IE8 are not well followed ECMAScript3 standard, however, the problem is handled correctly. Obviously, the ECMAScript5 specification is more in line with developers' expectations, that is, the same regular expression literally creates an independent RegExp object with different lastIndex to facilitate separate processing.

On the 247th page of The ECMAScript5 specification, there are two other instructions to illustrate the changes in the regular expression literal of ECMAScript5 and ECMAScript3:

7.8.5: Edition 5 requires early reporting of any possible RegExp constructor errors that wocould 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 wowould have been interpreted as the final character of the literal.

The first is when the literal expression in ECMAScript5 is converted to a RegExp object, any RegExp constructor error will be reported as soon as possible, in the previous specification, an error is reported only when the object is created and executed.

2nd means that the unescaped forward slash (/) in the regular expression literal of ECMAScript5 can be directly used in the regular expression character class. In ECMAScript3, it can only serve as the start and end characters of the regular expression literal. From IE6-IE9, Firefox3.6-Firefox4.0, Chrome and Safari, unescaped forward slash "/" can be used directly in the regular expression character class. For example:Copy codeCode: var my_regexp =/([8/5 + 4] *). {3}/g;
Var str = '2014 + 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

On the 76th page of JavaScript essence, it is also indicated that the forward slash "/" needs to be escaped in the character class of regular expressions, which is also based on the ECMAScript3 specification. Due to the fact that many special characters need to be escaped in a regular expression, you can use the Backslash "\" to ensure the security of any special characters in case of doubts, however, this rule is not suitable for letters and numbers.

The regular expression literal changes from ECMAScript3 to ECMAScript5 are quite in line with the two mentioned in HTML5 design principles. One is "in case of conflict, end users are given priority, followed by authors, followed by implementers, followed by standard makers, and finally theoretically completed", and the other is "supporting existing content ".

Finally, we recommend XRegExp, which is a very good JavaScript library for regular expressions. It is compatible with multiple 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.