开发者

Pick out part of a url with javascript regex?

If I have the following url:

http://www.youtube.com/watch?v=ysIzPF3BfpQ&feature=rec-LGOUT-exp_stro开发者_C百科nger_r2-2r-3-HM

or

http://www.youtube.com/watch?v=ysIzPF3BfpQ

How can I pick out just the 11 character string, ysIzPF3BfpQ?

Thanks for the help!


str.match(/v=(.*?)(&|$)/)[1];

It looks for a v=, then the shortest string of characters (.*?), followed by either a & or the end of the string. The [1] retrieves the first grouping, giving: ysIzPF3BfpQ.


To get the first capture group () from the URL that matches v=***********:

url.match(/v=(.{11})/)[1]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜