Getting the information from the HTTP User-Agent header using navigator.userAgent in JavaScript we get things like this (using different OS and browsers):
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12"
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.44 Safari/534.7"
"Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.7) Gecko/2009030423 Ubuntu/8.10 (intrepid) Firefox/3.0.7"
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.7) Gecko/2009021906 Firefox/3.0.7"
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/528.16 (KHTML, like Gecko) Version/4.0 Safari/528.16"
As you can see the OS version is the group of numbers before the third semicolon;
I need a javascript regex to get his numbers.
Thank you very much.
Getting the information from the HTTP User-Agent header using navigator.userAgent in JavaScript we get things like this (using different OS and browsers):
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12"
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.44 Safari/534.7"
"Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.7) Gecko/2009030423 Ubuntu/8.10 (intrepid) Firefox/3.0.7"
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.7) Gecko/2009021906 Firefox/3.0.7"
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/528.16 (KHTML, like Gecko) Version/4.0 Safari/528.16"
As you can see the OS version is the group of numbers before the third semicolon;
I need a javascript regex to get his numbers.
Thank you very much.
Share Improve this question asked Nov 22, 2010 at 14:26 jonagoldmanjonagoldman 8,75421 gold badges74 silver badges103 bronze badges 1-
Not an answer, so I'll ment: Remember to take any information in the
USER-AGENT
header with a grain of salt. Not only can it be faked, in some munities, it's likely to be. For a general-purpose website, though, you can largely ignore it. – T.J. Crowder Commented Nov 22, 2010 at 14:35
4 Answers
Reset to default 4A regex match approach is going to be awkward here. As you can see from your own samples, the version details for Firefox on Ubuntu are in a different place entirely. You could use a simple split regex which will separate the string into different parts:
// Split on ;, ( or ), removing the white-space at either side
var parts = navigator.userAgent.split(/\s*[;)(]\s*/);
Result:
["Mozilla/5.0", "Windows", "U", "Windows NT 6.1", "en-US", "AppleWebKit/534.7", "KHTML, like Gecko", "Chrome/7.0.517.44 Safari/534.7"]
The added benefit here is that you can extract the information you need without an overly plicated regular expression. A simple conditional from here could tell you where the OS/version data is stored, followed by further processing to extract just the version number. For example:
var result;
if (/^Linux/.test(parts[3]))
result = parts[6].split("/").pop(); // "8.10" (Ubuntu)
else
result = parts[3].split(" ").pop(); // "6.1" (Win 7)
Working demo: http://jsfiddle/AndyE/p6Uzc/
Further conditionals will be required for other browsers/systems (like browsers on mobile phones). For example, Opera 10 on Windows 7 has a user agent string containing:
Opera/9.80 (Windows NT 6.1; U; en) Presto/2.6.30 Version/10.63
Also remember that the USER AGENT string can be spoofed to look pletely different or contain different information.
Try this one:
/(?:[^;]+;){2}.*?([\w\.]+);/g
Within the first match you'll find the OS version
The following will grab all the non-space characters right in front of the third semicolon and put it in the first group:
.*?;.*?;.*?(\S+);
Depending on what you want to include you can change the \S to include only the characters you are interested in.
Solution that works for me:
// finding OS
function findOS(){
var OS_Name = navigator.appVersion;
if (OS_Name.indexOf("Win") != -1) {
// 64bit or 32bit version
if (test(/\sx64|\sx86|\swin64|\swow64|\samd64/i)) {
// if 64 bit Windows
} else {
// if 32 bit Windows
}
} else if (OS_Name.indexOf("Mac") != -1
|| OS_Name.indexOf("X11") != -1
|| OS_Name.indexOf("Linux") != -1
|| OS_Name.indexOf("SunOS") != -1 ) {
//if it's OS that is not Windows
}
}
function test(regex) {
return regex.test(navigator.userAgent);
}
Very elegant way to detect which OS client is using and is it 64 or 32bit.