识别当前是浏览器还是微信浏览器的问题

/mozilla|chrome|safari|firefox/.test(navigator.userAgent)

/mozilla|chrome|safari|firefox/.test(navigator.userAgent.toLowerCase()

这两个的值是相反的,

console.log(/mozilla|chrome|safari|firefox/.test(navigator.userAgent),'/mozilla|chrome|safari|firefox/.test(navigator.userAgent)')

console.log(/mozilla|chrome|safari|firefox/.test(navigator.userAgent.toLowerCase()),'/mozilla|chrome|safari|firefox/.test(navigator.userAgent.toLowerCase())')

这两个一个是true一个是false,

原因:

这两个表达式之所以产生不同的结果,是因为它们在匹配用户代理字符串时使用了不同的方法。

第一个表达式/mozilla|chrome|safari|firefox/.test(navigator.userAgent)是在原始的用户代理字符串中进行匹配。这意味着它只会检查用户代理字符串中是否包含"mozilla"、"chrome"、"safari"或"firefox"这些关键词。如果用户代理字符串中包含任何一个关键词,表达式将返回true,否则返回false

第二个表达式/mozilla|chrome|safari|firefox/.test(navigator.userAgent.toLowerCase())在匹配之前先将用户代理字符串转换为小写。这是因为用户代理字符串中的关键词可能以不同的大小写形式出现。通过将用户代理字符串转换为小写,我们可以确保匹配不受大小写的影响。

因此,如果用户代理字符串中包含"Mozilla"、"Chrome"、"Safari"或"Firefox"这些关键词,并且它们的大小写与表达式中的关键词匹配,那么第二个表达式将返回true。否则,它将返回false

请注意,用户代理字符串可能会因浏览器版本、操作系统或用户自定义而有所不同。因此,这些表达式只能作为一种简单的检测方法,并不能保证100%准确性

相关推荐
吴声子夜歌16 小时前
Linux命令——学习Shell(三)
linux·chrome·学习
panic了再查16 小时前
disable_reasons 里那一位早就置上了:把下架、禁用、装不上分成三层取证
chrome
头茬韭菜18 小时前
第 3 篇:「Pydantic 即 Schema」—— 工具生态三层解剖
前端·chrome·ai·openmanus
邋遢道1 天前
# 企业级 Agent 从 0 到 1(二):技术选型与最小骨架
前端·chrome
zh73143 天前
Go 1.23 → 1.26 升级检查文档
开发语言·chrome·golang
晴天163 天前
Chrome WebMCP 让网站学会向 AI「自我介绍」
前端·人工智能·chrome
全速向光3 天前
Firefox 火狐浏览器国际版:全平台开源浏览器的坚守与进化
开源·firefox
步行cgn4 天前
Spring Boot 中的 YAML 完全指南
android·spring boot·firefox
晴天164 天前
Chrome DevTools Protocol(CDP)分享-Day36
前端·chrome·chrome devtools
苏生Susheng4 天前
【软件实施】Linux系统Shell脚本教程
linux·运维·服务器·chrome·spring boot·学习·实施