/Safari\/\S+\s((?!Edge).)+/.test(navigator.userAgent) || 
/Mobile\/\S+\s((?!Safari).)+/.test(navigator.userAgent)

以上代码使用的一些特性

  1. iOS Safari 如果在 webview 模式下,userAgent 字符串不带 Safari 标识
  2. iOS Safari 的 Mobile 标识带版本号(Mobile/13C75 Safari/601.1)
  3. Android Webkit 和 Android Chrome 的 Mobile 标识不带版本号(Mobile Safari/534.30)
  4. 在 iOS 环境中即使是安装第三方浏览器(例如 Chrome,微信 webview)也只能调用 Safari 内核
  5. 微信、QQ 或者 Hybrid APP 一半都会在 userAgent 字符串最末尾加上环境标识
  6. Edge 浏览器直接使用了 Chrome 浏览器的 userAgent,只是在最后加上了 Edge/13.10586

附1:webview 浏览器 userAgent

# iOS Chrome:
Mozilla/5.0 (iPhone; CPU iPhone OS 9_2 like Mac OS X) AppleWebKit/601.1 (KHTML, like Gecko) CriOS/47.0.2526.107 Mobile/13C75 Safari/601.1.46

# iOS weixin:
Mozilla/5.0 (iPhone; CPU iPhone OS 9_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13C75 MicroMessenger/6.3.8 NetType/WIFI Language/zh_CN

# iOS QQ:
Mozilla/5.0 (iPhone; CPU iPhone OS 9_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13C75 QQ/6.1.0.496 Pixel/750 NetType/WIFI Mem/237

# Android weixin:
Mozilla/5.0 (Linux; U; Android 5.0.1; zh-cn; MX4 Build/LRX22C) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 MQQBrowser/5.4 TBS/025483 Mobile Safari/533.1 MicroMessenger/6.3.8 NetType/WIFI Language/zh_CN

# Android QQ:
Mozilla/5.0 (Linux; U; Android 5.0.1; zh-cn; MX4 Build/LRX22C) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 MQQBrowser/5.4 TBS/025483 Mobile Safari/533.1 V1_AND_SQ_6.1.0_312_YYB_D PA QQ/6.1.0.2635 NetType/WIFI WebP/0.3.0 Pixel/1152

附2:独立浏览器 userAgent

# iOS Safari:
Mozilla/5.0 (iPhone; CPU iPhone OS 9_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13C75 Safari/601.1

# Android Webkit:
Mozilla/5.0 (Linux; U; Android 4.0; en-us; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30

# Android Chrome:
Mozilla/5.0 (Linux; Android 5.0.1; MZ-MX4 Build/LRX22C) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/40.0.2214.114 Mobile Safari/537.36

# Android QQ:
Mozilla/5.0 (Linux; U; Android 5.0.1; zh-cn; MX4 Build/LRX22C) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 MQQBrowser/5.4 Mobile Safari/533.1

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

# Windows Microsoft Edge:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586