views:

207

answers:

1

I'm no good at Regular Expressions, really!

I would like to specifically detect WebKit browsers below version 525.

I have a regular expression [/WebKit\/[\d.]+/.exec(navigator.appVersion)] that correctly returns WebKit/5….…, really, I'd like it to return only the version number, but if the browser isn't WebKit, return null, or better still 0.

For example, if the browser was Trident, Presto or Gecko, return null, whereas if the browser is WebKit, return it's version number.

To clarify, I would like the regular expression to check if navigator.appVersion contains WebKit and if it does not, return null, if it does, return the version number.

I appreciate all your help!

Please let's keep this focused, let's not flirt with jQuery or the sort, it's overkill in this scenario.

+2  A: 

I'm providing this with the usual caveats: you should use this only if you are absolutely sure there is no way to test the actual feature you want to use that differs between WebKit versions:

function checkWebKit() {
    var result = /AppleWebKit\/([\d.]+)/.exec(navigator.userAgent);
    if (result) {
        return parseFloat(result[1]);
    }
    return null;
}
Tim Down
Is my <code>\d.</code> shorthand for your <code>0-9</code>?
Jonathon David Oates
Oh. Yes, good spot. Changing my answer.
Tim Down