admin管理员组文章数量:1336660
I found a method on the internet that can retrieve the Id of a youtube video from the url.
this is it.
var vid; var results; results = url.match("[\\?&]v=([^]*)"); vid = ( results === null ) ? url : results[1];
The Id will be contained in "vid".
What I don't understand and I find interesting and want to know is this.
results = url.match("[\\?&]v=([^]*)");
How does it work?
I found a method on the internet that can retrieve the Id of a youtube video from the url.
this is it.
var vid; var results; results = url.match("[\\?&]v=([^]*)"); vid = ( results === null ) ? url : results[1];
The Id will be contained in "vid".
What I don't understand and I find interesting and want to know is this.
results = url.match("[\\?&]v=([^]*)");
How does it work?
Share Improve this question edited Nov 19, 2009 at 0:49 bcat 8,9413 gold badges36 silver badges42 bronze badges asked Nov 19, 2009 at 0:30 Aaron de WindtAaron de Windt 17.7k14 gold badges50 silver badges67 bronze badges 02 Answers
Reset to default 12It's using a regular expression to extract the ID of the video from a plete URL. This particular regex breaks down as follows:
[\\?&]
is a character class. It matches a single character which is either&
or?
. (The question mark is a special character in JavaScript regexes, so it must be escaped by preceding it with a backslash. The backslash is a special character in JavaScript strings, so it must be escaped also, hence the double backslash. That's fairly mon in regular expressions.)v=
matches the literal stringv=
.(
begins a capturing group, so everything until the next)
will be placed into a separate entry in the returned array.[^&#]*
any number of characters (including none) until a&
or#
is found. The brackets indicate a character class as above, and the^
inverts the class so it includes all characters except those listed before the end bracket. The*
indicates that the preceding character class is to be matched zero or more times.- The
)
ends the capturing group.
Assuming the match is successful, results[0]
contains the entire URL, and results[1]
contains the contents of the first capturing group, i.e. the ID of the video.
This matches the video id in a youtube url.
[\\?&]v=
// finds the first ?v= or &v= in the query string
([^&#]*)
// matches everything else up to the next & or #
The video id is stored in results[1]
(assuming there was a match)
本文标签: javascriptI found this quotampv(amp*)quot on the internet can someone explain it to meStack Overflow
版权声明:本文标题:javascript - I found this "[\?&]v=([^&#]*)" on the internet can someone explain it to me - Sta 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742417479a2470995.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论