Skip to content
the poor coder

youtube url regex

Poor Man
Published
Reading time
2 min read
On this page

YouTube URL Regex#

As an avid user of YouTube, I have had to work with YouTube URLs quite a bit. Whether it's for embedding videos or sharing links with friends, it's important to understand the structure of a YouTube URL and how to properly validate it. In this post, I will be discussing how to use regular expressions (regex) to validate YouTube URLs.

What is a Regex?#

Regex is a pattern matching language used for searching and manipulating text. It allows you to specify a pattern of characters to match against a string. In the case of validating YouTube URLs, we can use regex to ensure that the URL is in the correct format.

The YouTube URL Format#

A typical YouTube URL looks like this:

Text
https://www.youtube.com/watch?v=dQw4w9WgXcQ

The syntax of a YouTube URL can be broken down into several parts:

  • https:// - The protocol used to access the website.
  • www.youtube.com - The domain name of the website.
  • /watch?v= - The path to the video resource.
  • dQw4w9WgXcQ - The unique identifier for the video.

Validating a YouTube URL with Regex#

To validate a YouTube URL using regex, we need to ensure that it matches the correct format. Here is an example regex pattern that matches a typical YouTube URL:

Text
/^(?:https?:\/\/)?(?:www\.)?youtube\.com\/watch\?v=([a-zA-Z0-9_-]{11})$/

Let's break down this pattern:

  • /^ - The beginning of the string.
  • (?:https?:\/\/)? - An optional group that matches the protocol (http or https) and the double slashes that precede the domain name.
  • (?:www\.)? - An optional group that matches the www subdomain.
  • youtube\.com\/watch\?v= - The path to the video resource.
  • ([a-zA-Z0-9_-]{11}) - The unique identifier for the video, which is exactly 11 characters long and can contain letters, numbers, underscores, and hyphens.
  • $/ - The end of the string.

We can use this pattern in JavaScript to validate a YouTube URL:

Text
const url = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';
const regex = /^(?:https?:\/\/)?(?:www\.)?youtube\.com\/watch\?v=([a-zA-Z0-9_-]{11})$/;
const isValidUrl = regex.test(url);
console.log(isValidUrl); // true

Alternative Regex Patterns#

There are several other regex patterns that can be used to validate YouTube URLs, depending on your specific needs. Here are a few examples:

  • Matches any YouTube URL:
Text
/^(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/watch\?v=|youtu\.be\/)([a-zA-Z0-9_-]{11})$/
  • Matches only YouTube video IDs:
Text
/^[a-zA-Z0-9_-]{11}$/

Conclusion#

Regex can be a powerful tool for validating YouTube URLs and ensuring that they are in the correct format. By using the patterns discussed in this post, you can ensure that your YouTube URLs are valid and avoid any errors when embedding videos or sharing links.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC