0

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?

14
  • How many matches do you expect? Do you also need to match DOQsYk8cbnE in https://www.youtube.com watch?v=DOQsYk8cbnE? Commented Mar 21, 2018 at 13:17
  • @WiktorStribiżew three matches: OovKTBO4aQs, DOQsYk8cbnE and 97aiSGxmizg Commented Mar 21, 2018 at 13:19
  • where you get this strange str? maybe you can ask for array instead? Commented Mar 21, 2018 at 13:20
  • Try regex101.com/r/GBSiXs/1 Commented Mar 21, 2018 at 13:20
  • if you are only getting youtube URLs, then why don't you just check for "v=" (some value) " " Commented Mar 21, 2018 at 13:20

4 Answers 4

1

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)

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

Comments

1

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);

Comments

0

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]);
  }
})

1 Comment

No need to split on space
-1

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))

1 Comment

As you can see in the minimal reproducible example I made from your answer, the result is not a list of IDs since you do not show the captures

Your Answer

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