IE用户代理正则表达式(包括IE11和兼容视图) [英] IE user agent regexp (including IE11 and compat view)

查看:521
本文介绍了IE用户代理正则表达式(包括IE11和兼容视图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要您的帮助来为用户代理字符串创建IE特定的正则表达式.我的目标是获取正确的IE版本(包括IE11),并检查浏览器是否正在运行"Compat View".

I need your help creating an IE specific regular expression for the user agent string. My goal is to get the correct IE version (including IE11), and also checking whether if the browser is running "Compat View".

因此,例如,我在正常模式下对IE9的期望结果:IE 9.0 和兼容视图"中的IE9:IE 9.0 CV

So, for example, my desired result for IE9 in normal mode: IE 9.0 And IE9 in "Compat View": IE 9.0 CV

如果不考虑兼容性,则此模式最多可用于IE 10: MSIE ([0-9]{1,}[\.0-9]{0,})

This pattern works for up to IE 10, when not taking compat view in to consideration: MSIE ([0-9]{1,}[\.0-9]{0,})

但是,compat视图中的IE9将具有类似于... MSIE 7.0 ... Trident/5.0的用户代理字符串.另外,IE11将不再在字符串中完全使用MSIE(请参见下面的示例).

However, IE9 in compat view will have a user agent string similar to ... MSIE 7.0 ... Trident/5.0. Also, IE11 will ni longer use MSIE at all in the string (see example below).

我对正则表达式不太了解,因此我需要您提供有关模式的帮助.尽管我认为三叉戟模式应该很明显,/Trident\/\d/.

I don't know regex very well, so I need your help with the pattern. Though I think the trident pattern should be pretty obvious, /Trident\/\d/.

我将在C#中使用它,所以我想我需要做这样的事情(欢迎提出建议):

I will be using this in C#, so I suppose I need to do something like this (suggestions welcome):

var regex = new Regex(@"MSIE ([0-9]{1,}[\.0-9]{0,})");
var match = regex.Match(Request.UserAgent);
if (match.Success)
    //handle match.Groups


一些有用的信息:


Some info that might be useful:

IE9的用户代理字符串:

类似于IE8,IE9的兼容性视图将映射到IE7标准模式,而在兼容性视图中,IE9的UA字符串将为:

Similar to IE8, IE9’s Compatibility View will map to IE7 Standards Mode, and IE9’s UA string when in Compatibility View will be:

Mozilla/4.0(兼容; MSIE 7.0; Windows NT 6.0; Trident/5.0) 在兼容性视图中,IE9通过应用程序版本号(Mozilla/4.0)和版本令牌(MSIE 7.0)将自身报告为IE7.这样做是为了兼容性. 三叉戟令牌从"Trident/4.0"增加到"Trident/5.0",使网站可以区分在Compat View中运行的IE9和在Compat View中运行的IE8.

Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/5.0) In Compatibility View, IE9 reports itself as IE7 through the application version number (Mozilla/4.0) and version token (MSIE 7.0). This is done for compatibility. An incremented Trident token, from ‘Trident/4.0’ to ‘Trident/5.0’, allows websites to distinguish between IE9 running in Compat View and IE8 running in Compat View.

IE11的用户代理字符串:

Windows 7 IE11示例:Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko

Windows 7 IE11 example: Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko

类似的有用线程

推荐答案

使用正则表达式模式

(?:\b(MS)?IE\s+|\bTrident\/7\.0;.*\s+rv:)(\d+)

您匹配所有当前已知的IE浏览器用户代理字符串,并且匹配#1组中确定的IE版本(如果未在兼容性视图中运行).

you match all current known IE browser user agent strings and the number matching in group #1 determinate version of IE (if not running in Compatibility View).

要了解IE是否正在兼容性视图中运行,请查找

To find out if IE is running in Compatibility View, lookup for match of

\bMSIE\s+7\.0;.*\bTrident\/(\d+)\.0

#1组中的匹配数字(在关键字"Trident"之后)确定兼容性视图中IE的版本,如下所示:

The number matching in group #1 (after keyword "Trident") determinate version of IE in Compatibility View as follow:

4 -> IE  8 in CV
5 -> IE  9 in CV
6 -> IE 10 in CV
7 -> IE 11 in CV

这篇关于IE用户代理正则表达式(包括IE11和兼容视图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆