Basically, the video id is preceded by v=
, and followed by either the end of the string, or &
. So the regex you're looking for is simply this:
var expr = /(?:v=)([^&]+)/;
console.log('http://www.youtube.com/watch?v=9bZkp7q19f0&desc=gangnam'.match(expr)[1]);
//logs "9bZkp7q19f0"
console.log('http://www.youtube.com/watch?desc=gangnam&v=9bZkp7q19f0'.match(expr)[1]);
//logs "9bZkp7q19f0"
You could (but it's not really required) make sure to only match the pattern above in the URI (the bit that follows a ?
in the full string:
var expr = /\?.*(?:v=)([^&]+)/;
The pattern you've tried is riddled with faults, and to begin fixing that is just too much work, I'd just forget about it. For example:
/youtu.be/
Matches a literal youtu
followed by one instance of any char (that isn't a new line) (.
), followed by a literal be
. Thus it matches youtu2be
, youtu#be
, youtu.be
, even youtu be
In response to your comment:
expr = /(youtu\.be\/|[?&]v=)([^&]+)/;
console.log('http://www.youtu.be/9bZkp7q19f0'.match(expr)[2]);
//logs "9bZkp7q19f0"
console.log('http://www.youtube.com/watch?desc=gangnam&v=9bZkp7q19f0'.match(expr)[2]);
//logs "9bZkp7q19f0"
console.log('http://youtu.be/9bZkp7q19f0'.match(/(youtu\.be\/|v=)([^&]+)/)[2]);
//logs "9bZkp7q19f0"
console.log(' youtube.com/watch?argv=xyz&v=u8nQa1cJyX8'.match(/(youtu\.be\/|[?&]v=)([^&]+)/)[2]);
//logs "u8nQa1cJyX8"
That's all. No need to check for a preceding ? or &...
How does it work:
(youtu\.be\/|[?&]v=)
: matches either literal youtu.be/
or either ?v=
or &v=
([^&]+)
: matches (and groups) everything that follows previous match, except for &
That means that youtu.be/<thiswillmatch>&<this will not match>
and youtube.com/foo/bar/watch?some=params&v=<this will match>&<this won't>
. It doesn't matter if the v=
bit is directly after the ?
or after an ampersand, all this regex is interested in is finding that v=
, and matching everythign that follows up until the first &
that follows. If it can't find the v=
, but youtu.be/
is found, the regex will capture everything after the forward slash (ie the vid id)