0

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?

6
  • Why do you need to detect a browser? Commented Apr 17, 2014 at 16:03
  • @NiettheDarkAbsol: Why does that matter? Commented Apr 17, 2014 at 16:04
  • I think you need test or match instead of indexOf Commented Apr 17, 2014 at 16:05
  • Unless you are absolutely sure what you are doing, prefer feature sniffing over browser sniffing. Commented Apr 17, 2014 at 16:05
  • 1
    I've been given some business requirements and I have to code this. Doesn't really matter the challenges. I'm asking for some RegExp help in looking for a number 17 and higher. Commented Apr 17, 2014 at 16:08

4 Answers 4

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.')

1 Comment

var bs nice touch! :)
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.