2

If I have the following url:

http://www.youtube.com/watch?v=ysIzPF3BfpQ&feature=rec-LGOUT-exp_stronger_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!

Andrew
  • 3,901
  • 15
  • 50
  • 64

2 Answers2

4
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.

David Tang
  • 92,262
  • 30
  • 167
  • 149
1

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

url.match(/v=(.{11})/)[1]
mVChr
  • 49,587
  • 11
  • 107
  • 104