问题描述
在 Internet Explorer 和 Microsoft Edge 上运行时,我需要打印浏览器名称和版本。以下代码适用于 chrome,但不适用于 IE 和 Edge。那么有人可以解释一下下面的代码(这些字符是什么 (?=/))/?\s*(\d+)/i),tem ..etc)。这段代码是别人写的。 比你
getbrowserVersion(){
var userAgent= navigator.userAgent,tem,matchTest= userAgent.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
if(/trident/i.test(matchTest[1])){
tem= /\brv[ :]+(\d+)/g.exec(userAgent) || [];
return 'IE '+(tem[1] || '');
}
if(matchTest[1]=== 'Chrome'){
tem= userAgent.match(/\b(OPR|Edge)\/(\d+)/);
if(tem!= null) return tem.slice(1).join(' ').replace('OPR','Opera');
}
matchTest= matchTest[2]? [matchTest[1],matchTest[2]]: [navigator.appName,navigator.appVersion,'-?'];
if((tem= userAgent.match(/version\/(\d+)/i))!= null) matchTest.splice(1,1,tem[1]);
return matchTest.join(' ');
}
解决方法
const isIEOrEdge = /msie\s|trident\/|edge\//i.test(window.navigator.userAgent)
我上面用过请尝试一次
,您可以使用像“Bowser”这样的库:https://github.com/lancedikson/bowser
const browser = Bowser.getParser(window.navigator.userAgent);
console.log(browser.getBrowser());
// outputs
{
name: "Internet Explorer"
version: "11.0"
}