I need to detect IE7 (and IE6) using the user agent string:
I have made the following regex:
navigator.userAgent.match(/MSIE [67]\./)
However, IE9 in quirks mode also matches the regex with the following user agent:
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 7.1; Trident/5.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C)
I could make two regexes:
navigator.userAgent.match(/MSIE [67]\./) !== null
&& navigator.userAgent.match(/Trident\/5\.0/) === null
Is there a way I can combine them into one?
Assuming the Trident part always comes after the MSIE part, you can use a lookahead:
I’m not familiar with user agents strings, but I’m guessing that maybe IE10 in quirks mode could have a Trident version > 5, so you could change it to:
UPDATE: second regex edited to include earlier versions of Trident too, e.g. Trident/4.0 in IE8, as well as potential later versions >= 10.
UPDATE2: Cleaned up RegEx’s to be valid in javascript.