Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Android OS version from user-agent

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?

like image 830
user605333 Avatar asked Mar 14 '11 00:03

user605333


People also ask

How to get Android version in JavaScript?

The we need to search if the 'android' keyword is present in the string or not to do that we will use indexOf. If it is present then get the version which is just after keyword 'android' by using . slice() and indexOf.

What is Android Useragent?

Published: 15 May 2022. The User-Agent (UA) string is contained in the HTTP headers and is intended to identify devices requesting online content. The User-Agent tells the server what the visiting device is (among many other things) and this information can be used to determine what content to return.

What is AppleWebKit 537.36 Khtml like Gecko used for?

AppleWebKit/537.36 indicates what browser rendering engine is used. A rendering engine is what transforms HTML into an interactive webpage on the user's screen. The WebKit browser engine was developed by Apple and is primarily used by Safari, Chromium, and all other WebKit-based browsers. (KHTML, like Gecko).

How do I know if my device is IOS or Android?

On your device, go to the Home screen (the one with all the icons), and tap on the Settings icon. Scroll down and tap on About phone or About tablet. Some information will appear. If one of the lines of information says Android with a version number, you have an Android device.


2 Answers

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.

like image 161
fearphage Avatar answered Oct 02 '22 16:10

fearphage


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

navigator.userAgent.match(/Android [\d+\.]{3,5}/)[0].replace('Android ','')
like image 41
eighteyes Avatar answered Oct 02 '22 16:10

eighteyes