3

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".

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

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

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).

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/.

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'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 example: Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko

Similar useful thread

jammus
  • 2,540
  • 23
  • 28
Johan
  • 35,120
  • 54
  • 178
  • 293
  • _Why_ do you need the browser version? Can't you use feature detection? – CodeCaster Nov 14 '13 at 20:14
  • @CodeCaster I want to keep track of what IE versions that are being used in my application. I don't want to use feature detection for that (for example, no activeX == IE11). – Johan Nov 14 '13 at 20:17

1 Answers1

12

Using regex pattern

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

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).


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

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

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
Ωmega
  • 42,614
  • 34
  • 134
  • 203
  • Thanks but how would this distinguish between IE9 and IE9 in compat view? It would just give me IE7 in the latter case... – Johan Nov 14 '13 at 20:29
  • Maybe I should post a new question about this, but how about `MSIE ([^;]*)|Trident.*; rv ([0-9.]+)`. MSIE part will match up to 10, otherwise it will go for the trident + rv version – Johan Nov 14 '13 at 20:50
  • Please note that `7 -> IE 11 in CV` is incorrect. Consider this IE11 string, not in CV `Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko` – Johan Nov 14 '13 at 20:55
  • 1
    @Johan - I am correct and you are incorrect. User agent string for IE11 in non-CV will not match my regex pattern for CV, because MSIE part is not present. – Ωmega Nov 14 '13 at 21:02
  • Oh right... Do you happen to know how an IE11 CV string looks like? – Johan Nov 14 '13 at 21:05
  • Btw, the last pattern doesn't seem to match. This is IE9 CV: http://regexr.com?376ra – Johan Nov 14 '13 at 21:09
  • 1
    @Johan - See http://blogs.msdn.com/b/ieinternals/archive/2013/09/21/internet-explorer-11-user-agent-string-ua-string-sniffing-compatibility-with-gecko-webkit.aspx – Ωmega Nov 14 '13 at 21:23