开发者

Javascript Reg Exp number in brackets

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜