Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

This should be very simple (when you know the answer). From this question

I want to give the posted solution a try. My question is:

How to get the parameter value of a given URL using JavaScript regular expressions?

I have:

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

I need:

Ahg6qcgoay4

I tried:

http://www.youtube.com/watch\?v=(w{11})

But: I suck...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
189 views
Welcome To Ask or Share your Answers For Others

1 Answer

You almost had it, just need to escape special regex chars:

regex = /http://www.youtube.com/watch?v=([w-]{11})/;

url = 'http://www.youtube.com/watch?v=Ahg6qcgoay4';
id = url.match(regex)[1]; // id = 'Ahg6qcgoay4'

Edit: Fix for regex by soupagain.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...