Reputation: 1288
I have a regex that is trying to look if the browser is IE 9 or above. I have a regex that will identity if it IE 9 but not IE 10.
Working to identity IE 9: MSIE ([0-9]{1,}[\.0-9]{0,})
Some of the IE 10 user agent text: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E)
Regex currently not identifying IE 9 or 10: MSIE ^(?:0|[1-9][0-9]?)$
This is being done in javascript, so here is the code for that:
function IsIE9() {
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer') {
var ua = navigator.userAgent;
//var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
var re = new RegExp("MSIE ^(?:0|[1-9][0-9]?)$");
if (re.exec(ua) != null)
rv = parseFloat(RegExp.$1);
}
return parseInt(rv) >= 9;
}
Can someone help me out on a regex that will identify an IE version and return the number value ( I would like to not hard code the IE values in there so it doesn't have to be updated for each new IE version.
---- UPDATE ----
Both answers seem to work. I went with this for my regex to get the version number: MSIE ((?:0|[1-9][0-9]?))
it returns the integer value.
Upvotes: 0
Views: 931
Reputation: 3771
Why don't you just extract the version number using a single regex. I looked here and it appears that all IE user agent strings are of the form "MSIE v.n;".
var re = /MSIE ([^;]*)/; // stop at the following semi-colon
var agentString = "agent string here";
var version = re.exec(agentString)[1]; // [1] is the capture from ()
var majorVersion = version.split('.')[0]; // "9" or "10"
Depending on what you are doing, though, you may want to consider feature-detection instead of browser sniffing, though.
Upvotes: 2
Reputation: 3532
remove the ^
and the $
, those are to indicate beginning and end of line
Upvotes: 0