Skip to content

Instantly share code, notes, and snippets.

@cookpete
Last active December 18, 2015 19:18
Show Gist options
  • Save cookpete/5831708 to your computer and use it in GitHub Desktop.
Save cookpete/5831708 to your computer and use it in GitHub Desktop.
// Build an object of versions (to the nearest float) from a user agent style string
// Supports irregular version numbers like 1_2_3
// parseVersions("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36");
// returns { "Mozilla": 5, "Intel Mac OS X": 10.8, "AppleWebKit": 537.36, "Chrome": 27, "Safari": 537.36 }
function parseVersions(string) {
var versions = {};
string.replace(/\b([a-z][a-z\s]+).\b(\d+[^a-z]\d+)/gi, function(match, software, version) {
versions[software] = parseFloat(version.replace(/\D+/g, '.'));
});
return versions;
}
navigator.versions = parseVersions(navigator.userAgent);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment