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!

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That's what I was going for. Much nicer and simpler than mine.
Can be simplified (and sped up) even more: str.match(/v=([^&]+))[1];
1

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

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

2 Comments

What if the url is just: youtube.com/watch?v=ysIzPF3BfpQ ?
Fixed it to just get the next 11 characters, so you have to assume that v= will never be blank.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.