1

detectmobilebrowsers.com provides this line for mobile detection on nginx:

if ($http_user_agent ~* "(android|bb\d+|meego).+mobile| ...

To enable tablet detection, they provide this line:

|android|ipad|playbook|silk

How can I detect phone or tablet, like at a finer grain? The issue is droid phone has a user agent string like "Android ... Mobile" and tablet just has "Android".

I looked at nginx if statements. There's no and operator, and nested if's lack documentation. I saw the NOT OR regex, and working on a solution using that:

Regular Expressions: Is there an AND operator?

Community
  • 1
  • 1
stampede76
  • 1,521
  • 2
  • 20
  • 36

1 Answers1

1

If you pan to match android that is not followed with mobile word, you can use a negative lookahead based regex pattern:

android(?!.*mobile)

or a version with word boundaries to only check and match whole words:

\bandroid\b(?!.*\bmobile\b)

See the regex demo

The negative lookahead contains a .*mobile pattern meaning that if the android is followed with any 0+ characters followed with mobile, the match should fail.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563