0

I'm using the PHP class "Mobile Detect" to detect user device type (tablet/phone) however I also want to detect certain devices in javascript. Can someone help me convert the following into javascript regex (just need a true/false return)?

var user_agent = navigator.userAgent;

var samsung = 'Galaxy.*Tab|SAMSUNG.*Tablet|Galaxy.*Tab|Android.*GT-';
// if statement checking samsung regex against user agent

var nexus = '^.*Android.*Nexus(((?:(?!Mobile))|(?:(\s(7).+))).)*$';
// if statement checking nexus7 regex against user agent
Joe
  • 1,762
  • 9
  • 43
  • 60

1 Answers1

0

Are you looking for something like this?

var user_agent = navigator.userAgent;

var samsung = /Galaxy.*Tab|SAMSUNG.*Tablet|Galaxy.*Tab|Android.*GT-/i;
// if statement checking samsung regex against user agent
if (samsung.test(user_agent)) {
   // This is a samsung mobile device
}

var nexus = /^.*Android.*Nexus(((?:(?!Mobile))|(?:(\s(7).+))).)*$/i;
// if statement checking nexus7 regex against user agent
if (nexus.test(user_agent)) {
   // This is a nexus mobile device
}

Or did you actually want help fixing malfunctioning / incomplete regular expressions?

If you need a solid regular expression, I suggest you go play around at txt2re.com it's the best regular expression generator I know of.

Robin van Baalen
  • 3,632
  • 2
  • 21
  • 35
  • Perfect! I've always struggled with regex, bookmarked that site as well for future assistance :) – Joe Jan 24 '13 at 18:54