Regex URL for YouTube channel URL.

how can i check YouTube channel url using REGEX?

I found this pattern but it doesn't work as expected

/((http|https):\/\/|)(www.|)youtube\.com\/(channel\/|user\/|)[a-zA-Z0-9]{1,}/

      

Can anyone help me?

+3


source to share


2 answers


Your problem is the extra pipe after user\/

Here's the corrected regex:

((http|https):\/\/|)(www\.|)youtube\.com\/(channel\/|user\/)[a-zA-Z0-9\-]{1,}

      



The reason this is a problem is because it makes (channel | user) optional.

Also, use a question mark for optional instead of an additional channel, so instead of ((http|https):\/\/)?

((http|https):\/\/)?

((http|https):\/\/|)

+8


source


To get the channel name or channel ID from the YouTube url use:

(?:https|http)\:\/\/(?:[\w]+\.)?youtube\.com\/(?:c\/|channel\/|user\/)?([a-zA-Z0-9\-]{1,})

      

Works for:



The channel identifier starts with "UC". I don't know of any other way to recognize channel names and channel names.

0


source







All Articles