function getUA(OSArray) {
var OSMap = {},
UA = navigator.userAgent.toLowerCase(),
EvalStr = '';
//如果不传参或者传参不为数组或者为空数组,默认返回所有操作系统信息
if (OSArray instanceof Array === false || OSArray.length < 1) {
OSArray = UA.match(/(\S*)\/(\S*)/g);
OSArray.forEach(function(v, i) {
OSMap[OSArray[i].split('/')[0]] = OSArray[i].split('/')[1];
})
} else {
OSArray.forEach(function(v, i) {
if (UA.indexOf(v) > -1) {
EvalStr = "UA.match((/(\\S*" + v + ")\\/(\\S*)/g) || [, '\'])[0]";
OSMap[v] = eval(EvalStr).split('/')[1];
} else {
OSMap[v] = false;
}
})
}
return OSMap;
}
// var tr = ua.match(/(\S*mozilla)\/(\S*)/g)
var UaMyInfo = getUA(['applewebkit', 'safari', 'micromessenger']);
console.log(UaMyInfo);
var UaAllInfo = getUA();
console.log(UaAllInfo);