gpt4 book ai didi

c# - IE 用户代理正则表达式(包括 IE11 和紧凑 View )

转载 作者:行者123 更新时间:2023-11-30 19:41:24 25 4
gpt4 key购买 nike

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

因此,例如,我在正常模式下对 IE9 的期望结果:IE 9.0以及“兼容 View ”中的 IE9:IE 9.0 CV

当不考虑兼容 View 时,此模式适用于 IE 10:MSIE ([0-9]{1,}[\.0-9]{0,})

但是,兼容 View 中的 IE9 将具有类似于 ... MSIE 7.0 ... Trident/5.0 的用户代理字符串。此外,IE11 将不再在字符串中使用 MSIE(请参见下面的示例)。

我不是很了解正则表达式,所以我需要你的模式帮助。虽然我认为三叉戟模式应该很明显,/Trident\/\d/

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

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

一些可能有用的信息:

IE9's user agent string :

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 (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's user agent string:

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

Similar useful thread

最佳答案

使用正则表达式模式

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

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


要查明 IE 是否在兼容性 View 中运行,查找匹配项

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

在第 1 组中匹配的数字(在关键字“Trident”之后)确定兼容性 View 中的 IE 版本,如下所示:

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

关于c# - IE 用户代理正则表达式(包括 IE11 和紧凑 View ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19987447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com