OTUser
OTUser

Reputation: 3848

Javascript RegEx to detect browser

I am trying to detect opera safari firefox browsers using a regex but Chrome also have safari in userAgent value so am trying to ignore chrome from userAgent as below but it isn't working, can someone help me out?

var userAgent = navigator.userAgent.toLowerCase();
var isUnsupportedBrowser = (/(opera|safari|firefox|(?!chrome))\/?\s*(\.?\d+(\.\d+)*)/i).test(userAgent);

Upvotes: 1

Views: 3097

Answers (2)

d0nut
d0nut

Reputation: 2834

If you want to only match UAs that don't contain chrome this should work:

/(?!.*chrome).*/i

Regex101

If you really want to validate that the UA contains Opera, Firefox, or Safari you can use the following:

/(?=.*(opera|safari|firefox))(?!.*chrome).*/i

Upvotes: 2

Giuseppe Ricupero
Giuseppe Ricupero

Reputation: 6272

If you simply want to exclude Chrome use a regex that matches its UA:

var isUnsupportedBrowser = (/Chrome.*Safari/).test(userAgent);

If you instead want to match all the allowed UA, provide some test cases.

Upvotes: 0

Related Questions