You don't have to know. Regular expressions _ Regular expressions for JavaScript

Source: Internet
Author: User
These two days are using regular expressions to make a slightly more complicated thing, but the difference between different browsers can waste a lot of my ginseng.

Now I put the regular expression in the five major browsers (IE, Firefox, Chrome, Safari, Opera, whichever is the current version) to sort out the differences, for everyone, but also for me to make a memo.

1. Firefox and Chrome will be overly tuned for regular expressions created in loops (and in the definition of functions embedded in them), as if they were assuming that the person writing JavaScript would write down the construction and assignment of regular expressions in the wrong place.
Copy Code code as follows:

var R;
for (var i = 0; i < 2; i++) {
var x =/abc/g;
if (R)
document.write (r = = x); Firefox and Chrome will output "true" when the second loop
Else
R = x;
}

2. If you pass a function that is likely not to return as the second parameter to the Replace method, ie may delete the matching text directly (in the previous test I concluded that opera's behavior is quite unique and now seems to be the wrong conclusion), Other browsers will replace the matching text with "undefined".
Copy Code code as follows:

document.write (' 123 '. Replace (/2/, function () {})); IE will output "13", while other browsers output "1undefined3"

3. If you use an existing regular expression instance as a parameter when you create a regular expression using the form new RegExp, most browsers create an entirely new, completely separate, regular expression instance with the same basic functionality. And Safari simply returns the regular expression instance as a parameter.
Copy Code code as follows:

var r =/1/;
document.write (new RegExp (r) = = R); Safari outputs "true" and the other browsers output "false"

4. If you convert a "null" regular expression directly into a string, browsers other than IE will get "/(?:) /", and IE gets"//"--but when you extract the Source property directly from the regular expression, you get an empty string.
Copy Code code as follows:

document.write (New RegExp (")); IE Output "//", other browser output "/(?:) /”
document.write (eval (' + New RegExp ("))//IE Output" undefined ", other browser output"/(?:) /”

5. If you convert a regular expression that contains a slash "/" directly into a string--take "new RegExp ('/") as an example, only Firefox and opera will get "/\//", and extracting the source property directly will get "\/", while other browsers will get "// /", and extracting the source property directly will get"/".
Copy Code code as follows:

document.write (New RegExp ('/')); Firefox and opera output "/\//", other browsers output "///"
document.write (eval (' + New RegExp ('/'))//Firefox and opera output "/\//", other browsers output "undefined"

6. If you use an invalid option flag (such as "/abc/n") when you use a literal expression to define a regular expression, chrome and safari completely ignore this invalid option flag (equivalent to "/abc/"), while other browsers can cause syntax errors.
Copy Code code as follows:

document.write (/abc/n); Chrome and Safari output "/abc/" and syntax errors are generated in other browsers

7. If you create a regular expression by using a constructor, the (second) string parameter for which you specify an option flag contains a character that is not a valid flag, causing an exception in Firefox, and other browsers ignoring the invalid part.
Copy Code code as follows:

document.write (New RegExp (' 1 ', ' n ')); Firefox will cause an exception, indicating invalid flags, and other browsers will output "/1/"

For the time being, find the new and fill it up.
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.