Skip to content

Instantly share code, notes, and snippets.

@ticky
Last active October 19, 2023 23:13
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ticky/3909462 to your computer and use it in GitHub Desktop.
Save ticky/3909462 to your computer and use it in GitHub Desktop.
Browser and Version regex

Browser and Version Regular Expression 2.0

(MSIE|(?!Gecko.+)Firefox|(?!AppleWebKit.+Chrome.+)Safari|(?!AppleWebKit.+)Chrome|AppleWebKit(?!.+Chrome|.+Safari)|Gecko(?!.+Firefox))(?: |\/)([\d\.apre]+)

This regular expression is capable of retrieving the browser and version for the following browsers;

  • Internet Explorer
  • Firefox (INCLUDING alpha and "pre" versions)
  • Other browsers reporting a "Gecko" version in their user agent
  • Chrome
  • Safari
  • Other browsers reporting an "AppleWebKit" version in their user agent

Returns the following strings;

  1. Browser Name ("Safari", "Chrome", "Firefox", etc.)
  2. Browser Version* ("26.0.1410.64")

It gracefully handles browsers with confusing user-agents (for example Chrome, which reports Chrome, Safari and AppleWebKit in one go) for supported combinations.

* Note that the "Browser Version" returned on Safari is currently the WebKit version, not the Browser version.

@geoffcallender
Copy link

geoffcallender commented Nov 2, 2017

Thank you, this is great - except, it doesn't handle IE Edge or 11. Try "(MSIE|Trident|(?!Gecko.+)Firefox|(?!AppleWebKit.+Chrome.+)Safari(?!.+Edge)|(?!AppleWebKit.+)Chrome(?!.+Edge)|(?!AppleWebKit.+Chrome.+Safari.+)Edge|AppleWebKit(?!.+Chrome|.+Safari)|Gecko(?!.+Firefox))(?: |\/)([\d\.apre]+)".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment