user1781710
user1781710

Reputation:

Detecting specific browser with JavaScript RegExp

I have a some code like this:

var versionPattern = new RegExp("^Firefox/[1]{1}[7-9]{1}|[2-9]{1}[0-9]{1}$");

if (navigator.userAgent.indexOf(versionPattern) > 0) {
        alert("Firefox Detected");
        return true;

I want to detect versions of Firefox 17 and up but this code doesn't seem to be working correctly. What can I do to improve the RegExp?

Upvotes: 0

Views: 2640

Answers (4)

Smi Lee
Smi Lee

Reputation: 138

You can test your regular expression with *REGEX*.test('testString'); it returns true / false. I could find strings like "Firefox17" "Firefox22" ... by this regular Expression : /^(Firefox)[1]{1}[7-9]{1}|[2]{1}[0-9]{1}$ so you might try that : /^(Firefox)[1]{1}[7-9]{1}|[2]{1}[0-9]{1}$.test(navigator.userAgent)

Upvotes: 0

kennebec
kennebec

Reputation: 104780

You can read the userAgent string and match the version number-

var bs=navigator.userAgent.match(/Firefox\/(\d+)/);
if(bs && parseInt(bs[1])>17) alert('Firefox '+bs[1]+' detected.')

Upvotes: 0

Pedro Lobito
Pedro Lobito

Reputation: 98921

The variable you need is var majorVersion

The code example below uses navigator.userAgent to implement browser detection. It also uses navigator.appName and navigator.appVersion as a last resort only, if the userAgent string has an "unexpected" format. In your browser, this code produces the following output:

Browser name = Mozilla Firefox
Full version = 28.0
Major version = 28
navigator.appName = Netscape
navigator.userAgent = Mozilla/5.0 (Windows NT 6.2; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0

And here is the source code that performed the browser detection:

DEMO

   <script>  
    var nVer = navigator.appVersion;
    var nAgt = navigator.userAgent;
    var browserName  = navigator.appName;
    var fullVersion  = ''+parseFloat(navigator.appVersion); 
    var majorVersion = parseInt(navigator.appVersion,10); // this is what you need
    var nameOffset,verOffset,ix;


// In Firefox, the true version is after "Firefox" 
if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
 browserName = "Firefox";
 fullVersion = nAgt.substring(verOffset+8);
}
// In most other browsers, "name/version" is at the end of userAgent 
else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < 
          (verOffset=nAgt.lastIndexOf('/')) ) 
{
 browserName = nAgt.substring(nameOffset,verOffset);
 fullVersion = nAgt.substring(verOffset+1);
 if (browserName.toLowerCase()==browserName.toUpperCase()) {
  browserName = navigator.appName;
 }
}
// trim the fullVersion string at semicolon/space if present
if ((ix=fullVersion.indexOf(";"))!=-1)
   fullVersion=fullVersion.substring(0,ix);
if ((ix=fullVersion.indexOf(" "))!=-1)
   fullVersion=fullVersion.substring(0,ix);

majorVersion = parseInt(''+fullVersion,10);
if (isNaN(majorVersion)) {
 fullVersion  = ''+parseFloat(navigator.appVersion); 
 majorVersion = parseInt(navigator.appVersion,10);
}

document.write(''
 +'Browser name  = '+browserName+'<br>'
 +'Full version  = '+fullVersion+'<br>'
 +'Major version = '+majorVersion+'<br>'
 +'navigator.appName = '+navigator.appName+'<br>'
 +'navigator.userAgent = '+navigator.userAgent+'<br>'
)
</script>

SRC

Upvotes: 0

putvande
putvande

Reputation: 15213

Something like:

navigator.userAgent.match(/Firefox\/([1]{1}[7-9]{1}|[2-9]{1}[0-9]{1})/);

Don't use the ^ in your RegExp because Firefox is not the beginning of the String. And if you want to use the optional 17-19 | 20 - 99, it should be between ( and ).

indexOf is to find the first index of a string inside another string.

Upvotes: 0

Related Questions