User Agent parsing with Regex

梦想与她 提交于 2019-12-04 15:45:12

It's not actually possible to "reliably" parse a user-agent string; several common User-Agent strings violate HTTP 1.1 (I forget the RFC number) WRT the characters allowed between the parentheses (. or / or ; or something?). User-Agent sniffing is pretty fragile when you want to "whitelist" certain features and leads to complaints about preferential treatment of some browsers over others (especially when it's Microsoft doing it), and means that someone has to keep giant regex updated.

Is there really no better way (e.g. with JavaScript?) to detect the features that the browser supports?

Nevertheless, you can do something like ; *CPU +iPhone +OS +(4_(2|[3-9]|\d\d)|[5-9]|\d\d)\[0-9a-zA-Z_]* +like +Mac +OS +X *;.

While this technically could be done, you would have to list all the possible future version numbers explicitly. regex is a text matching tool; there's no easy way to include logic such as "return true if the number is greater than this, false if smaller". You would probably want to just extract the number string ([0-9]+_[0-9]+ or something) and then do the logic on the output of that.

([5-9]|\d\d)[^+]*

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!