Skip to content

Instantly share code, notes, and snippets.

@dalethedeveloper
Created December 20, 2011 21:00
Show Gist options
  • Save dalethedeveloper/1503252 to your computer and use it in GitHub Desktop.
Save dalethedeveloper/1503252 to your computer and use it in GitHub Desktop.
Mobile Device Detection via User Agent RegEx

#Mobile Device Detection via User Agent RegEx

Yes, it is nearly 2012 and this exercise has been done to death in every imaginable language. For my own purposes I needed to get the majority of non-desktop devices on to a trimmed down, mobile optimized version of a site. I decided to try and chase down an up-to-date RegEx of the simplest thing that could possibly work.

I arrived at my current solution after analyzing 12 months of traffic over 30+ US based entertainment properties (5.8M+ visitors) from Jan - Dec 2011.

The numbers solidified my thoughts on the irrelevancy of including browsers/OSes such as Nokia, Samsung, Maemo, Symbian, Ipaq, Avant, Zino, Bolt, Iris, etc. The brass tacks of the matter is that you certainly could support these obscure beasts, but are you really going to test your site on them? Heck, could you even find one?! Unless the folks that pay you are die hard Treo users my guess is "No".

Interestingly enough my research shows that /Mobile/ is more efficient than /iP(hone|od|ad)/ since it catches all Apple's device-specific Safari builds along with a slew of miscellaneous browsers. This also includes several Android builds, and Windows Mobile as well.

Note: I didn't bother with /i (case insensitive) on the RegExes below since user agents don't typically change the case they transmit.

The 90% Solution

This should have been obvious (and I'm just being nice to BlackBerry)

/Mobile|Android|BlackBerry/

The Overly Explicit 90% Solution

/Mobile|iP(hone|od|ad)|Android|BlackBerry|IEMobile/

The 98% Solution

Includes barely significant outliers and third party browsers. Anything that doesn't make this cut probably can't render any website made past 2004 anyway.

/Mobile|iP(hone|od|ad)|Android|BlackBerry|IEMobile|Kindle|NetFront|Silk-Accelerated|(hpw|web)OS|Fennec|Minimo|Opera M(obi|ini)|Blazer|Dolfin|Dolphin|Skyfire|Zune/

Other People's Solutions

####Google Reinforcing the /Mobile/ trick

####CakePHP 1.2

####CakePHP 2.0

####Symphony Extensions

####Ruby / Rack

####Detect Desktop First (Flip the flop)

####User Agent Resources

####Riffraff

@danelowe
Copy link

danelowe commented Oct 2, 2014

Assuming a mobile-sized android has both Android and Mobile in the UA string (an assumption made by most introductions to Rails Action Pack Variants), the following could be helpful in matching Android phones but not Android tablets.

/(?=.*\bAndroid\b)(?=.*\b(m|M)obile\b).*/

@MaffooBristol
Copy link

@danelowe Thanks for that, though there's a little mistake- the second part should be a negative lookahead, not positive. Also, why not just make the matching case insensitive rather than using (m|M) and the like?

Amended version:

/(?=.*\bandroid\b)(?!.*\bmobile\b).*/i

Edit:

Actually, it appears that that even gets some false positives, for example LG Optimus tablets seem to deliver the user agent Mozilla/5.0 (Linux; U; Android 2.2; en-us; LG-P990/V08c Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 MMS/LG-Android-MMS-V1.0/1.2, which contains the word android twice, meaning that it matches the lookahead on MMS/LG-Android-MMS-V1.0/1.2. Checking for the Android 2.2 part makes it a bit more solid.

/(?=.*\bandroid\b\s\d(\.\d)*)(?!.*\bmobile\b).*/i

@GabrielDelepine
Copy link

@MaffooBristol Your regex /(?=.*\bandroid\b\s\d(\.\d)*)(?!.*\bmobile\b).*/i seams wrong for iPad 3/4 ``userAgent="5.0 (iPad; CPU OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53"and BlackBerry PlaybookuserAgent="Mozilla/5.0 (PlayBook; U; RIM Tablet OS 2.1.0; en-US) AppleWebKit/536.2+ (KHTML like Gecko) Version/7.2.1.0 Safari/536.2+"`

@GabrielDelepine
Copy link

The best regex I have found to detect tablet is /(tablet|ipad|playbook|silk)|(android(?!.*mobile))/i (you need to test the user agent.)

Tested with :
Nexus 5
Nexus 10
Nexus 7 2
iPhone 4
iPhone 6 Plus
iPad 3/4
Nokia Lumia
BlackBerry Z10
BlackBerry Playbook
Amazon Kindle Fire 7
HTC Evo
LG Optimus LTE
Motorola Droid X
Samsung Galaxy Note 3
Samsung Galaxy S4
Sony Xperia Z
Chrome 41 Desktop

@gdibble
Copy link

gdibble commented Apr 22, 2016

ty for isTablet @GabrielDelepine

@theodiablo
Copy link

theodiablo commented May 13, 2016

Hi!

I would also add the two following words to find in the mobile detection regex:
Nokia
UCWEB

This is to match the two following examples found on this project

JUC (Linux; U; 2.3.6; zh-cn; GT-I8150; 480*800) UCWEB8.7.4.225/145/800
This i a UC Browser 8.7 on a Samsung Galaxy W running Android 2.3.6

Mozilla/5.0 (Series40; Nokia501/10.0.2; Profile/MIDP-2.1 Configuration/CLDC-1.1) Gecko/20100401 S40OviBrowser/3.0.0.0.73
This i a Nokia Xpress 3.0.0 on a Nokia Asha 501 running Nokia Asha Platform

@alexnoise79
Copy link

Just a question, did you ever considered the inverse solution?

I mean, everithing that isNOT.

like:

!window.navigator.userAgent.match(/MSIE|Trident|Firefox|Chrome|Safari|Edge/)

should work or i'm missing something?

@cghislai
Copy link

cghislai commented May 5, 2020

should work or i'm missing something?

You could very well have mobile chromes and safari and whatnot specifying 'Chrome', 'Safari' and 'WhatNot' in their ua.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment