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?