使用正则表达式从 User-Agent-Header 解析浏览器版本
解析用户代理标头我需要查明我是否正在处理 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您转义反斜杠,您的模式应该可以工作,或者您指的是 JavaScript?。 (在这种情况下它应该起作用)
说明:
(?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)
Explanation:
(?i)
makes the match case insensitiveMSIE
matches the string MSIE\\s+
one or more spaces[5-8]
matches digits 5 to 8\\.
match a dot\\d+
one or more digits您可以使用字符组来匹配版本,fi
[678]\.0
。You can use character groups to match versions, f.i.
[678]\.0
.