Details about how to judge the code of various browsers using JS Regular Expressions

Source: Internet
Author: User

All annotations are in Code . Very detailed.
I only judged ie Firefox Google because I didn't install any other browsers, so haha. Of course, if you want to judge other browsers. The basic code is the same. Copy code The Code is as follows: (function (){
Window. sys = {};
VaR UA = navigator. useragent. tolowercase ();
// First, let's take a look at what the three browsers return.
// Ie UA = Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; qqpinyin 730;. Net CLR 1.1.4322)
// Firefox UA = Mozilla/5.0 (windows; U; Windows NT 5.2; ZH-CN; RV: 1.9.2) Gecko/20100115 Firefox/3.6
// Chrome UA = Mozilla/5.0 (windows; U; Windows NT 5.2; en-US) applewebkit/532.5 (khtml, like gecko) Chrome/4.0.249.0 Safari/532.5

// You can see the preceding Browser ie msie 8.0 Firefox/3.6 Chrome/4.0.249
// We only need to use regular expressions to find out which browser can match the key points in the string.
// See the following regular Firefox Regular Expression/Firefox \/([\ D \.] + )/
// Firefox \/matches a character like Firefox/
// ([\ D \.] +) () indicates the character to be captured. It is his credit to use [1] to obtain the version number.
// [] Indicates that \ D \. is included as a whole.
// \ D match a number \. Match A. Add together to match 8. Something like this
// [] + [] There is a + outside, which will repeat the originally matched items in [] and turn it into this 8.8.8.8. Of course, this can also be 48.1.563
SYS. Firefox = UA. Match (/Firefox \/([\ D \.] + )/);
SYS. Ie = UA. Match (/MSIE \ s ([\ D \.] + )/);
// MSIE \ s ([\ D \.] +)
// IE is basically the same as Firefox.
// MSIE 8.0 \ s is a space matching between MSIE and 8.0
SYS. Chrome = UA. Match (/chrome \/([\ D \.] + )/);
// Chrome \/([\ D \.] +)
// Same as Firefox
})();

// Use
If (SYS. Firefox ){
Alert (true)
} Else {
Alert (false)
}
If (SYS. ie [1] = "8.0 "){
Alert (true)
} Else {
Alert (false)
}

Alert (SYS. Firefox );
Alert (SYS. ie );
Alert (SYS. Chrome );
// Only the current browser's sys can use [1] to return the version number
Alert (SYS. Firefox [1]);
// Alert (SYS. ie [1]);
// Alert (SYS. Chrome [1]);

I encountered a problem. I can see that the experts wrote this.

Firefox \/([\ D.] +) // expert
Firefox \/([\ D \.] +) // my
// I am confused about the differences between the two. I used a lot of STR tests. I didn't see any difference. // is [\ D.] + the same as [\ D \.] +? Please give answers.

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.