jonagoldman
jonagoldman

Reputation: 8754

RegEx to detect OS version in JavaScript

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.

Upvotes: 2

Views: 10259

Answers (4)

Ivan Aracki
Ivan Aracki

Reputation: 5361

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.

Upvotes: 0

Andy E
Andy E

Reputation: 344585

A 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 complicated 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.net/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 completely different or contain different information.

Upvotes: 4

Anton Hansson
Anton Hansson

Reputation: 2171

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.

Upvotes: 0

mck89
mck89

Reputation: 19241

Try this one:

/(?:[^;]+;){2}.*?([\w\.]+);/g

Within the first match you'll find the OS version

Upvotes: 0

Related Questions