17

I've been trying to find a parser or regex that will give me the Android OS version from a user agent string.

E.g.

Mozilla/5.0 (Linux; U; Android 2.2.1; fr-fr; Desire HD Build/FRG83D) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

Will return:

2.2.1

Can anyone help?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user605333
  • 303
  • 1
  • 4
  • 9

5 Answers5

24

This regular expression is a bit more "future proof" than mathepic's answer:

Android (\d+(?:\.\d+)*);

It allows for multiple digits in each place as well as additional periods in the version number. Android's been out 3 years and we're on 3.0. Eventually we'll get to 10.0.0.

This will catch all of the following:

  • 1.6 (real)
  • 2.3.4 (real)
  • 9
  • 12.34.56 (fake. but one day...)
  • 3.4.5.6 and 4.5.6.7.8.9 (fake... but just in case)

This could be written a little more strictly as:

Android (\d+(?:\.\d+){0,2});

This sticks more closely to the schema we've already seen used, but could potentially miss some versions of they decide to add an additional .1 at the end of a version. It also matches for the future 10.0.0 version.

fearphage
  • 16,808
  • 1
  • 27
  • 33
  • Careful: this will also match "DefinitelyNotAndroid 0.0" ;-) You might want to append a `\b` to the front. – Joachim Sauer May 11 '11 at 09:26
  • And since we're talking about future-proofing: I'd avoid hard-coding `;` and replace it with `[;)]` or `\b`. A theoretical paranoid version *might* use `Mozilla/5.0 (Linux; U; Android 2.2.1)`. – Joachim Sauer May 11 '11 at 09:28
  • 1
    Fair enough, +1 I suppose. It looks a little nicer but I also think its somewhat more difficult to understand. In any case I probably should have used +'s on my digits. – alternative May 11 '11 at 10:55
  • @Joachim Sauer: If anything, I'd solve it going the other direction. `; Android (\d+(?:\.\d+)+)` which might not be a bad call. I don't really see them getting rid of the build number or phone name though. – fearphage May 11 '11 at 12:25
  • 1
    Seems Android 9 skips .0 >> Mozilla/5.0 (Linux; Android 9; SM-G960F Build/PPR1.180610.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/73.0.3683.90 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/218.0.0.46.109;] – Damjan Apr 27 '19 at 07:33
  • 1
    Does not catch Android 10 it catch Android 10.x but It fails on "Android 10" – Berkay Yıldız Feb 01 '20 at 15:30
  • Thank you. I've updated the regular expressions to catch major versions with no periods. – fearphage Feb 02 '20 at 16:57
2

May I present an answer that others can easily copy and paste.

navigator.userAgent.match(/Android [\d+\.]{3,5}/)[0].replace('Android ','')
eighteyes
  • 1,306
  • 1
  • 11
  • 18
2

Motorola's player user agents can have the following:

Linux;Android ; Release/4.1.2

So, I've had to start using the the following:

[a|A]ndroid[^\d]*([\d[_|.]]+\d)
andy
  • 53
  • 3
1

Regex should work, something along the untested lines of

Android ([0-9]\.[0-9](\.[0-9])?);

And then use whatever regex function you use to get that part inside the parens.

alternative
  • 12,703
  • 5
  • 41
  • 41
  • This will fail for `Android 1.5`. – Macarse Mar 14 '11 at 00:30
  • @Marcarse you are right, you will have to adjust for that. I wasn't sure if there was a guaranteed at least a .0 on the end. The edit should fix that issue. – alternative Mar 14 '11 at 00:48
  • I'd recommend that you avoid capturing unnecessarily in regex. It's just a good practice and I've been told in some languages it offers performance benefits. – fearphage May 11 '11 at 06:47
0

If you want to use it with User-Agent it might be safe but else you shouldn't use it.

Android ((\d+|\.)+[^,;]+)

It catches everything after Android it works with,

1.6 2.4.4 10.12.5

To test with Live Regex: https://www.phpliveregex.com/p/uPw

Berkay Yıldız
  • 440
  • 3
  • 14