使用正则表达式从 User-Agent-Header 解析浏览器版本

发布于 2025-01-06 04:56:35 字数 328 浏览 1 评论 0原文

解析用户代理标头我需要查明我是否正在处理 IE-8 或更早版本:

<= IE8

因此正则表达式在以下情况下应返回 true:

(compatible; MSIE 8.0;...)
(compatible; MSIE 7.0;...)
etc. 

以下应产生 false:

(compatible; MSIE 9.0;...)

以下正则表达式不起作用:

"MSIE [6-8]\."

Parsing the user-agent header I need to find out if I am dealing with IE-8 or earlier:

<= IE8

So the regex should return true in the following cases:

(compatible; MSIE 8.0;...)
(compatible; MSIE 7.0;...)
etc. 

The following should yield false:

(compatible; MSIE 9.0;...)

This following regex does not work:

"MSIE [6-8]\."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃气十足 2025-01-13 04:56:35

如果您转义反斜杠,您的模式应该可以工作,或者您指的是 JavaScript?。 (在这种情况下它应该起作用)

"(?i)MSIE\\s+[5-8]\\.\\d+"

说明:

  • (?i) 使匹配不区分大小写
  • MSIE 与字符串 MSIE
  • \\s+ 匹配 1 或更多空格
  • [5-8] 匹配 5 到 8 个数字
  • \\. 匹配一个点
  • \\d+ 一个或多个数字

Your pattern should work if you escape the backslash, or did you mean JavaScript?. (in which case it should work)

"(?i)MSIE\\s+[5-8]\\.\\d+"

Explanation:

  • (?i) makes the match case insensitive
  • MSIE matches the string MSIE
  • \\s+ one or more spaces
  • [5-8] matches digits 5 to 8
  • \\. match a dot
  • \\d+ one or more digits
谢绝鈎搭 2025-01-13 04:56:35

您可以使用字符组来匹配版本,fi [678]\.0

You can use character groups to match versions, f.i. [678]\.0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文