开发者

Javascript Reg Exp number in brackets

开发者 https://www.devze.com 2023-02-11 19:51 出处:网络
if (!$is.IE5) { 开发者_如何学Pythonv = (ua.toLowerCase().match(new RegExp(\".+(?:rv|it|ra|ie)[\\\\/: ]([\\\\d.]+)\"))||[])[1];
if (!$is.IE5) {
  开发者_如何学Python  v = (ua.toLowerCase().match(new RegExp(".+(?:rv|it|ra|ie)[\\/: ]([\\d.]+)"))||[])[1];
}

What does [1] mean in this reg expression??


It is an array deference on the answer.

v = (ua.toLowerCase().match(new RegExp(".+(?:rv|it|ra|ie)[\\/: ]([\\d.]+)"))||[])[1];

The deference [1] is applied to the function result, to get the first matched group (groups are delimited with parentheses ()).

So v = the first group match of (ua.toLowerCase().match(new RegExp(".+(?:rv|it|ra|ie)[\\/: ]([\\d.]+)"))||[]).

Note the ||[] at the end which allows for no matches to not give an error.

This first group match is the ([\\d.]+) group, the first parentheses is not stored due to the (?:...) non-matching group construct.

0

精彩评论

暂无评论...
验证码 换一张
取 消