Differences between browsers using JavaScript Regular Expressions

Source: Internet
Author: User

The results of Regular Expressions in JavaScript may be different in different browsers. The following describes the regular expressions in five mainstream browsers (IE, Firefox, Chrome, Safari, Opera, take the current version as the standard.

1. firefox and Chrome will over-optimize the regular expressions created in loops (and built-in function definitions, it seems that the person who writes JavaScript will write the construction and assignment of the regular expression wrong.

Var r; for (var I = 0; I <2; I ++) {var x =/abc/g; if (r) // In the second loop, Firefox and Chrome will output "true" document. write (r = x); else r = x ;}

2. if you pass a function that may not return anything as the second parameter to the replace method, then IE may directly Delete the matched text (in the previous test, I summarized that Opera's behavior is unique and it seems to be a wrong conclusion now ), other browsers Replace the matched text with "undefined ".

// IE outputs "13", while other browsers output "1undefined3" document. write ('20140901'. replace (/2/, function (){}));

3. if you use an existing regular expression instance as a parameter when creating a regular expression in the form of new RegExp, most browsers will create a basic function with the same, but completely independent, brand-new regular expression instances; while Safari simply returns the regular expression instance as a parameter.

Var r =/1/; // Safari outputs "true", while other browsers output "false" document. write (new RegExp (r) = r );

4. If a blank regular expression is directly converted into a string, browsers other than IE will get "/(? :)/", And IE will get" // "-- but when the source attribute is directly extracted from the regular expression, all the results are null strings.

// IE outputs "//" and other browsers outputs "/(? :)/"Document. write (new RegExp (''); // IE outputs "undefined" and other browsers outputs "/(? :)/"Document. write (eval ('' + new RegExp ('')))

5. if a regular expression containing the slash "/" is directly converted into a string -- Take "new RegExp ('/')" as an example, only Firefox and Opera get "/\/", and directly extract the source attribute to get "\/", while other browsers get "//". and directly extract the source attribute to get "/".

// Firefox and Opera output "//", and other browsers output "//" document. write (new RegExp ('/'); // Firefox and Opera output "//", other browsers output "undefined" document. write (eval (''+ new RegExp ('/')))

6. if an invalid option flag (for example, "/abc/n") is used to define a regular expression using a literal expression "), chrome and Safari will ignore this invalid option flag (equivalent to "/abc/"), while other browsers will cause syntax errors.

// Chrome and Safari will output "/abc/", and other browsers will generate the syntax error document. write (/abc/n );

7. if the (second) string parameter that specifies the option flag for a regular expression by using the constructor contains invalid flag characters, an exception will occur in Firefox, other browsers will ignore invalid parts.

// An exception occurs in Firefox, indicating that the flag is invalid. other browsers will output "/1/document. write (new RegExp ('1', 'n '));"

Related Article

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.