You may not know the regular expression about JavaScript.

Source: Internet
Author: User

These two days I have been using regular expressions to create something a little complicated, but the difference between different browsers can waste a lot of people.

Now, let's sort out the differences between the regular expressions in five mainstream browsers (ie, Firefox, chrome, Safari, and opera, based on the current version, I can also make a memo for myself.

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.CopyCodeThe Code is as follows: var R;
For (VAR I = 0; I <2; I ++ ){
VaR x =/ABC/g;
If (r)
Document. Write (r = x); // In the second loop, Firefox and chrome will output "true"
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 ".Copy codeThe Code is as follows: document. write ('20140901 '. replace (/2/, function () {}); // ie outputs "13", while other browsers output "1undefined3"

3. If an existing regular expression instance is used as a parameter when creating a regular expression in the form of new Regexp, most browsers will create a basic function that is the same, but completely independent, brand-new regular expression instances; while safari simply returns the regular expression instance as a parameter.Copy codeThe Code is as follows: var r =/1 /;
Document. Write (New Regexp (R) = R); // safari outputs "true" while other browsers outputs "false"

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.Copy codeThe Code is as follows: Document. Write (New Regexp (''); // ie outputs" // "and other browsers outputs"/(? :)/"
Document. Write (eval (''+ new Regexp ('') // ie outputs "undefined" and other browsers outputs "/(? :)/"

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 "/".Copy codeThe Code is as follows: Document. Write (New Regexp ('/'); // Firefox and opera output "//", and other browsers output "//"
Document. Write (eval (''+ new Regexp ('/') // output" // "for Firefox and opera, and" undefined "for other browsers"

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.Copy codeThe Code is as follows: Document. Write (/ABC/n); // chrome and Safari will output "/ABC/". syntax errors are generated in other browsers.

7. If the regular expression is created by the constructor, the (second) string parameter that specifies the option flag for it contains the characters of the invalid flag, an exception occurs in Firefox, while other browsers ignore invalid parts.Copy codeThe Code is as follows: document. write (New Regexp ('1', 'n'); // an exception is thrown in Firefox, indicating that the flag is invalid. other browsers will output "/1 /"

for the time being, make up a new one.
from: http://www.cnblogs.com/NanaLich

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.