Regex to detect IE 5, 6, 7 and 8 but no others

落爺英雄遲暮 提交于 2019-12-12 12:18:08

问题


I've put together this regex which should only return a match where the user agent string is IE 5, 6, 7 and 8. Obviously one of the main goals here is to avoid the confusing IE11 user agent string..

Is this Regex valid (have i missed something)?

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

UPDATE

The below matches all user agent strings for 5, 6, 7 and 8. And none of 9, 10. It does however match one for IE11, returned from .Net using Request.UserAgent

'Mozilla/4.0 (compatible; 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\)).*$

回答1:


Here is a list of user agent strings across IE versions. To validate the IE 5, 6, 7 and 8 user agent string, you only need to check for "Trident/6.0)" at the end.

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

See regex101 example.

EDIT:

According to 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

So, the regex should check if the MSIE version is not followed by 'Trident/5.0', up to 'Trident/7.0': ^.*MSIE [5-8](?:\.[0-9]+)?(?!.*Trident\/[5-9]\.0).*$. It will be necessary to update it when 'Trident/10'+ is released. Or, here it is: ^.*MSIE [5-8](?:\.[0-9]+)?(?!.*Trident\/(?:[5-9]|1[0-9])\.0).*$.



来源:https://stackoverflow.com/questions/29296524/regex-to-detect-ie-5-6-7-and-8-but-no-others

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!