1

I have want to create a regex pattern that will provide a match for a safari user agent string. The problem is that Chrome also seems to match:

Safari Regex:

(?!Chrome).*Safari

This still matches against a Chrome user agent string, but I thought that the negative look ahead would resolve this issue?

Chrome user agent:

Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36

Safari user agent:

Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27

Can anyone provide me with a regex solution for this?

Update:

The answer I was directed to at Efficient User-Agent Regex to find Safari in Python does not provide a regex solution for this problem. In fact, it doesn't address the issue at all, so I am suprised it was accepted as an answer

Thanks

2

5 Answers 5

2

You may use

^(?!.*Chrome).*Safari

See the regex demo.

Details

  • ^ - start of a string
  • (?!.*Chrome) - fails the match if there is Chrome substring after 0+ chars other than line break chars
  • .* - any 0+ chars other than line break chars
  • Safari - Safari substring.

Update (fixing issues in your answer)

Regex for Safari:

^(?!.*(?:Chrome|Edge)).*Safari

Regex for Chrome:

^(?!.*Edge).*Chrome

Regex for Edge:

/Edge/
Sign up to request clarification or add additional context in comments.

3 Comments

Excellent. And thank you for the explanation. Much appreciated. I will accept this as the answer...
@CharlesRobertson Great, please do.
Others answers are great, but this is the right one for the question.
1

This works:

^(?!.*\bChrome).*$

Demo

Or,

 ^(?!.*\bChrome).*\bSafari

If you are looking for the Safari match without Chrome specifically

The issue with your regex (?!Chrome).*Safari is you need the .* to match what is in front of the Chrome in the negative look assertion at the start of the string.

1 Comment

Brilliant. Thanks for this, and to think the moderator was pointing me to a previous post with an accepted answer that was completely off topic. I knew there was a regex solution, out there...
1

Building on @Wiktor Stribiżew & @dawg excellent answers, I felt it might be useful for anyone looking for a complete solution to the 3 browsers that contain 'safari' in their user agent string:

Regex for Safari:

^(?!.*Chrome|Edge).*Safari

Regex for Chrome:

^.*Chrome(?!.*Edge)

Regex for Edge:

Edge

1 Comment

I added fixes for the issues in this answer to my answer.
0

Have you tried this basic one? It matches Safari only.

.*(Safari).*

See test on regex101.

Comments

0

The answers above are correct for desktop and maybe Android. For iOS:

^(?!.*CriOS).*Safari

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.