user9062413
user9062413

Reputation:

Parsing Youtube URLs

I'm trying to get all Youtube video IDs from string like this:

https://www.youtube.com/watch?v=OovKTBO4aQs https://www.youtube.com/watch?v=DOQsYk8cbnE https://www.youtube.com/watch?v=97aiSGxmizg

Following to this answers I wrote code:

var re = /(?:https?:\/\/)?(?:youtu\.be\/|(?:www\.)?youtube\.com\/watch(?:\.php)?\?.*v=)([a-zA-Z0-9\-_]+)/g,
    str = 'https://www.youtube.com/watch?v=OovKTBO4aQs https://www.youtube.com/watch?v=DOQsYk8cbnE https://www.youtube.com/watch?v=97aiSGxmizg',
    match;

while (match = re.exec(str)) {
   if (match.index === re.lastIndex) {
      re.lastIndex++;
   }

   console.log(match[1]);
}

But console.log shows only last ID 97aiSGxmizg. What am I doing wrong?

Upvotes: 0

Views: 180

Answers (4)

Asons
Asons

Reputation: 87191

Based on the posted string's format, v=id, one can do something as simple as split the string at space and the again, combined with reduce(), at v=, to get the successfully split'ed id's.

I also used an anonymous function (function(){...})(); to only have to run the split once.

Stack snippet

var str = 'https://www.youtube.com/watch?v=OovKTBO4aQs https://www.youtube.com/watch?v=DOQsYk8cbnE https://www.youtube.com/watch?v=97aiSGxmizg';

var list = str.split(' ').reduce(function(r, e) {
  (function(i){
    if (i.length > 1) r.push(i[1]);
  })(e.split('v='));  
  return r;
}, []);

console.log(list);


As mentioned, if there are other formats, one can easily use a regex, e.g.

Stack snippet

var str = 'https://www.youtube.com/watch?v=OovKTBO4aQs https://www.youtube.com/watch?v=DOQsYk8cbnE https://www.youtube.com/watch?v=97aiSGxmizg http://www.youtube.com/v/-wtIMTCHWuI http://youtu.be/-DOQsYk8cbnE';

var list = str.split(' ').reduce(function(r, e) {
  (function(i){
    if (i.length > 1) r.push(i[1]);
  })(e.split(/v=|v\/-|be\/-/));
  return r;
}, []);

console.log(list);

Upvotes: 1

Shrihari Balasubramani
Shrihari Balasubramani

Reputation: 907

You regex is not correct.

The correct regex would be like this:

var re = /(?:https?:\/\/)?(?:youtu\.be\/|(?:www\.)?youtube\.com\/watch(?:\.php)?\?[^ ]*v=)([a-zA-Z0-9\-_]+)/g;

var str = 'https://www.youtube.com/watch?v=OovKTBO4aQs jiberish https://www.youtube.com/watch?v=DOQsYk8cbnE  jiberish a https://www.youtube.com/watch?v=97aiSGxmizg'
console.log(str.match(re))

Upvotes: -1

mplungjan
mplungjan

Reputation: 177786

Assuming v=something, try this (regex from Extract parameter value from url using regular expressions)

var regex = /\?v=([a-z0-9\-]+)\&?/gi, matches = [], index=1;
urls = "https://www.youtube.com/watch?v=OovKTBO4aQs https://www.youtube.com/watch?v=DOQsYk8cbnE https://www.youtube.com/watch?v=97aiSGxmizg";
while (match = regex.exec(urls)) matches.push(match[index])
console.log(matches)

Upvotes: 1

kfairns
kfairns

Reputation: 3057

The capture group will only match the last match in that string.

Split the strings into an array and log them there:

var re = /(?:https?:\/\/)?(?:youtu\.be\/|(?:www\.)?youtube\.com\/watch(?:\.php)?\?.*v=)([a-zA-Z0-9\-_]+)/g,
  str = 'https://www.youtube.com/watch?v=OovKTBO4aQs https://www.youtube.com/watch?v=DOQsYk8cbnE https://www.youtube.com/watch?v=97aiSGxmizg',
  strs = str.split(' ');


strs.forEach((str) => {
  var match;
  while (match = re.exec(str)) {
    if (match.index === re.lastIndex) {
      re.lastIndex++;
    }
    console.log(match[1]);
  }
})

Upvotes: 0

Related Questions