Mohamed Mohamed - 29 days ago 23
PHP Question

Regex for youtube channel URL

how can I validate youtube channel URL using REGEX ?

I found this pattern but it doesn't work properly

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


Can anyone help me ?

Answer

Your problem is the extra pipe after user\/

Here is the corrected regex:

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

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

Also, consider using a question mark for optional instead of a extra pipe, so like this ((http|https):\/\/)? instead of this ((http|https):\/\/|)

Comments