Regex for youtube channel URL

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.

A better way to write this regex is

(https?:\/\/)?(www\.)?youtube\.com\/(channel|user)\/[\w-]+

To get the channel name or channel id from a youtube URL use:

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

Works for:

  • https://www.youtube.com/user/channelblabla
  • https://www.youtube.com/channel/channelblabla
  • https://www.youtube.com/c/channelblabla
  • https://www.youtube.com/channelblabla

Channel ID's start with 'UC'. I don't know of any other way to recognize channel ID's vs. channel names.

Tags:

Php

Regex