2

How to validate a youtube playlist url using regex?
I've found the answer for validating video from other question..
/^http:\/\/(?:www\.)?youtube.com\/watch\?(?=.*v=\w+)(?:\S+)?$/

But I've just unable to validate a url like this :
http://www.youtube.com/watch?list=PL1F9CA2A03CF286C2&v=pFS4zYWxzNA&
or
http://www.youtube.com/watch?v=pFS4zYWxzNA&list=PL1F9CA2A03CF286C2&

4 Answers 4

12

I ended up with a something similar but different:

 /^.*(youtu.be\/|list=)([^#\&\?]*).*/

worked against the following urls:

Given the follow code:

        var regExp = /^.*(youtu.be\/|list=)([^#\&\?]*).*/;
        var match = url.match(regExp);
        if (match && match[2]){
            return match[2];
        }
        return null;

My return is PLBOh8f9FoHHjOz0vGrD20WcTtJar-LOrw

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

Comments

4

try this

^(https|http):\/\/(?:www\.)?youtube\.com\/watch\? #you forgot to mask the dot before com
(                                         #may lead to wrong parsing
    (v=.*&list=.*)| #v and then list
    (list=.*&v=.*)  #or versa verse - feel free to use better matching
)                   #for ids like "pFS4zYWxzNA"
(&.*)*$             #all other kinds of parameter

Edit: i improved the matching

^(https|http):\/\/(?:www\.)?youtube\.com\/watch\?
(?:&.*)*                         #extra params at the beginning
(
    (?:
         v=([a-zA-Z0-9_\-]{11})     #propper mathing for id
         (?:&.*)*                #extras
         &list=([a-zA-Z0-9_\-]{18}) #list
    )
    | 
    (?:
         list=([a-zA-Z0-9_\-]{18})
         (?:&.*)*                #versa
         &v=([a-zA-Z0-9_\-]{11})
    )
)
(?:&.*)*                         #extras at the end
(?:\#.*)*$                       #anchors

3 Comments

Yes, this regex works. However, the last capture group will always be empty because the .* at the end of each alternative matches everything up to the end of the string. Also, the use of the dot is inappropriate because it matches whitespace which is not valid in any URL. To improve this, I would replace all dot-stars with: [^&\s]* Otherwise, nice job.
@ridgerunner: I did try to change .* to become [^&\s]*, but it doesn't work.. I change it into this /^http:\/\/(?:www\.)?youtube\.com\/watch\?((v=[^&\s]*&list=[^&\s]*)|(list=[^&\s]*&v=[^&\s]*))(&[^&\s]*)*$/
@ridgerunner:i added the last group to handle youtuves extra params like &feature=list_related&playnext=1, also see edit
0

First validate YouTube url:

^https?\:\/\/(?:www\.youtube(?:\-nocookie)?\.com\/|m\.youtube\.com\/|youtube\.com\/)?(?:ytscreeningroom\?vi?=|youtu\.be\/|vi?\/|user\/.+\/u\/\w{1,2}\/|embed\/|watch\?(?:.*\&)?vi?=|\&vi?=|\?(?:.*\&)?vi?=)([^#\&\?\n\/<>\"']*)

then find the list:

[\?|&](list=)(.*)\&

Comments

0

I ended up with a something similar but different:

/[?&]list=([^#?&]*)/

worked against the following urls:

Given the follow code:

const listID = url.match(/[?&]list=([^#?&]*)/)?.[1];

console.log(listID);

My return is PLC18xlbCdwtTc61wuDviNoKmNUAVFT50D

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.