javascript - 从 JavaScript 中的用户代理信息获取设备名称
问题描述
我知道关于这个话题有很多问题,但没有一个对我有帮助。我知道,我无法通过 iPhone 等 JavaScript 获取设备。
所以当我运行这个命令时:
navigator.userAgent
我得到这个结果:
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
现在我想提取Macintosh
左括号后的第一个单词并将其存储在一个变量中。例如,我如何使用 RegEx 来做到这一点?
解决方案
RegExp is all about defining the logic of your pattern. This means you need to be sure about the string format for it to work.
So for example, in the example you posted, perhaps we define the bit you want to match as:
- "the first word after the first opening bracket"
In which case:
navigator.userAgent.match(/^[^\(]+\((\w+)/);
推荐阅读
- apache-zookeeper - 无法以独立模式启动 Zookeeper 服务器
- c++ - C ++内联汇编循环通过二维数组
- swift - 错误:尝试从第 1 节中删除第 0 行,但在更新之前只有 1 节(Swift 和 Realm)
- javascript - 插件或扩展 - DOM 元素 - .value
- c++ - 使用 Minimax 算法的 4x4 TicTacToe
- java - HttpUrlConnection - 离线缓存数据
- css - 在 scss 导入上设置 `font-display: swap`
- powershell - IIS 10 - 使用 Powershell 导入 SSL 证书 - “指定的登录会话不存在”
- delphi - TGaussianBlurEffect 图像质量低
- java - ArrayList.clone() 方法在 Java 中如何工作?