正则表达式检测 IE 5、6、7 和 8,但不检测其他版本

标签 regex browser-detection

我已经整理了这个正则表达式,它应该只返回用户代理字符串为 IE 5、6、7 和 8 的匹配项。显然,这里的主要目标之一是避免令人困惑的 IE11 用户代理字符串..

这个正则表达式有效吗(我错过了什么)吗?

.*MSIE [5-8]\b((?!Trident/4.0)).*

更新

下面的内容匹配 5、6、7 和 8 的所有用户代理字符串。9、10 都不匹配。但是,它确实匹配 IE11 的一个,使用 Request.UserAgent 从 .Net 返回

'Mozilla/4.0(兼容;MSIE 7.0;Windows NT 6.3;WOW64;Trident/7.0;.NET4.0E;.NET4.0C;.NET CLR 3.5.30729;.NET CLR 2.0.50727;.NET CLR 3.0.30729)'

^.*MSIE [5-8](?:\.[0-9]+)?(?!.*Trident\/6\.0\)).*$

最佳答案

这是一个list of user agent strings across IE versions 。要验证 IE 5、6、7 和 8 用户代理字符串,您只需检查末尾的“Trident/6.0)”。

使用此正则表达式:^.*MSIE [5-8](?:\.[0-9]+)?(?!.*Trident\/[5-9]\.0)。 *$

参见regex101 example .

编辑:

根据MSDN User Agent documentation ,

When the F12 developer tools are used to change the browser mode of Internet Explorer, the version token of the user-agent string is modified to appear so that the browser appears to be an earlier version.

This is done to allow browser specific content to be served to Internet Explorer and is usually necessary only when websites have not been updated to reflect current versions of the browser.

When this happens, a Trident token is added to the user-agent string.

This token includes a version number that enables you to identify the version of the browser, regardless of the current browser mode.

Token         Description
Trident/7.0   IE11
Trident/6.0   Internet Explorer 10
Trident/5.0   Internet Explorer 9
Trident/4.0   Internet Explorer 8

因此,正则表达式应检查 MSIE 版本后面是否没有“Trident/5.0”,最多为“Trident/7.0”: ^.*MSIE [5-8](?:\.[0 -9]+)?(?!.*三叉戟\/[5-9]\.0).*$。当“Trident/10”+发布时,有必要对其进行更新。或者,这里是: ^.*MSIE [5-8](?:\.[0-9]+)?(?!.*三叉戟\/(?:[5-9]|1[0-9])\.0).*$.

关于正则表达式检测 IE 5、6、7 和 8,但不检测其他版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29296524/

相关文章:

php - 如何使用正则表达式从HTML获取所有YouTube iframe

javascript - 如何判断一个 Promise 是否被浏览器支持

javascript - 使用 navigator.userAgent 进行 javascript 浏览器检测的语法解释

Javascript 正则表达式 : match string beginning with X, 不包括 Y,以 A 或 B 结尾

java - 正则表达式模式中的点行为

mysql - 从双引号的 csv 字段中去除可变数量的逗号

javascript - 使用 javascript 检测手机/平板电脑/网络客户端

javascript - 如何在不检查 HTTP header 的情况下检测移动浏览器?

facebook - 禁用 Facebook 移动浏览器检测?

java - 如何检查该行与正则表达式(regex)不矛盾?