JS regular expression judge each browser code detailed explanation

Time:2021-4-14

The comments are all in the code. It’s very detailed.
I only judged ie, Firefox and Google, because I didn’t install other browsers, so ha ha. Of course, if you want to judge other browsers. The basic code is the same.

Copy codeThe code is as follows:
(function(){
window.sys={};
var ua=navigator.userAgent.toLowerCase();
//First of all, let’s take a look at what the three browsers have returned.
//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 browser ie MSIE 8.0 firebox / 3.6 Chrome / 4.0.249 above
//We just need to use regular to find out which browser can match these key points in the string.
//Take a look at the regular expression of regular firebox / firebox \ / ([\ D \.] +)/
//Firebox \ / matches a character like firebox
//([[D \.] +) () indicates that the matching characters need to be captured. Later, you can use [1] to get the version number, which is his credit.
//It means to take the whole as a whole
//\D match a number. Match one. Add it together and you can match something like 8
//There’s a + on the outside of the [] +]. He will repeat the original matching things in the [], so it becomes 8.8.8.8. Of course, it can also be 48.1.563
sys.firefox=ua.match(/firefox\/([\d\.]+)/);
sys.ie=ua.match(/msie\s([\d\.]+)/);
//msie\s([\d\.]+)
//IE’s is basically the same as that of Firefox, there’s no big difference
//MSIE 8.0 is to match a space between MSIE and 8.0
sys.chrome=ua.match(/chrome\/([\d\.]+)/);
//chrome\/([\d\.]+)
//It’s the same as fox
})();

//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 sys of the current browser can use [1] to return the version number
alert(sys.firefox[1]);
//alert(sys.ie[1]);
//alert(sys.chrome[1]);

I have a problem. I saw the master write like this

Firebox \ / ([\ D.] +) / / expert’s
Firebox \ / ([\ D \.] +) / / my
//I wonder what the difference is between the two. I used a lot of STR tests. I don’t see any difference / / is it the same as [\ D.] +? Ask the master to answer.