0

Could anyone help me with a regexExp for targeting all mobile browsers apart from safari? saw this online but its turning out not to be that helpful

^(?:.*Windows Phone.*|.*WPDesktop.*|.*RIM.*|.*BlackBerry.*|.*Opera Mini.*|.*Opera Mobi.*|.*Silk\/.*|.*Kindle.*|.*Android.*|(?!.*?(?:iPhone)).*?Mobile.*|.*Symbian.*)$

Kofi
  • 9
  • 2
  • Is the string you try to evaluate always a mobile browser? 'Cause in that case it's gonna be easier to do the regex like "not Safari" instead of listing everything (which seems complicated) – Eselfar Jun 01 '17 at 16:11
  • @Eselfar yes its always a mobile browser – Kofi Jun 01 '17 at 18:18

1 Answers1

-1

You can try this regex:

(?:(?!Safari).)*

It includes everything but Safari. Examples:

String s1 = "My browser is Safari.";
boolean res1 = s1.matches("(?:(?!Safari).)*"); // res1 = false


String s2 = "My browser is Opera.";
boolean res2 = s2.matches("(?:(?!Safari).)*"); // res2 = true

Have a look to this answer for more info: https://stackoverflow.com/a/977290/1827254

Eselfar
  • 3,759
  • 3
  • 23
  • 43
  • It also matches chrome – Rani Jan 03 '18 at 06:17
  • @Rani Of course it matches Chrome! OP wanted `all mobile browsers apart from safari`. Maybe you should read the question before downvoting the answers... – Eselfar Jan 03 '18 at 10:41
  • The word Safari appears also on Chrome's user agent and therefore it doesn't work – Rani Jan 03 '18 at 13:52
  • @Rani So yeah, doesn't work as all the browsers based on Safari include 'Safari' in [there user agent](http://www.useragentstring.com/pages/useragentstring.php?typ=Browser). – Eselfar Jan 03 '18 at 14:11
  • @Rani Not great but an idea could be to list all the mobile browsers based on Safari and check if the user agent doesn't contain Safari or is not a browser of this list. But it could lead to some issues as you could have missed one browser in your list. But at least it should cover the most popular ones. Have a look to [this question](https://stackoverflow.com/questions/5899783/detect-safari-using-jquery) too – Eselfar Jan 03 '18 at 14:18