How to detect IE7 by the user agent string, leaving out newer IE versions running in quirks mode
javascript, regex, user-agent
Solution
Assuming the Trident part always comes after the MSIE part, you can use a lookahead:
/MSIE [67]\.(?!.*Trident[5]\.0)/
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:
/MSIE [67]\.(?!.*Trident[1-9])/
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.
Problem
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?