Start free · Free tools · Fair usage limits
Browse Tools

Categories

Custom AI Solutions – AI2Flows
LIVE NEWS
Loading latest AI & SEO news…
Home›Tools›Regex Tester›Regex for YouTube video URL
Regex pattern · validation

Regex for YouTube video URL

Regex for YouTube video ID: a tested pattern with examples, a plain-English breakdown, and code for JavaScript, Python, PHP, Java and Go.

Quick answer

Use the pattern below as a YouTube video URL regex. It matches "https://www.youtube.com/watch?v=dQw4w9WgXcQ" and "https://youtu.be/dQw4w9WgXcQ?t=42", and rejects "https://vimeo.com/123456" and "https://www.youtube.com/watch?v=short". Read the video ID from capture group 1.

Last updated · Maintained by FreeAIToolz · How we test

/^(?:https?:\/\/)?(?:www\.|m\.)?(?:youtube\.com\/(?:watch\?(?:.*&)?v=|embed\/|shorts\/)|youtu\.be\/)([A-Za-z0-9_-]{11})(?:[?&#].*)?$/
  • https://www.youtube.com/watch?v=dQw4w9WgXcQmatches
  • https://youtu.be/dQw4w9WgXcQ?t=42matches
  • https://www.youtube.com/shorts/dQw4w9WgXcQmatches
  • https://www.youtube.com/watch?list=PL1&v=dQw4w9WgXcQmatches
  • youtube.com/embed/dQw4w9WgXcQmatches
  • https://vimeo.com/123456no match
  • https://www.youtube.com/watch?v=shortno match
  • https://www.youtube.com/channel/UC123no match
Open in the full Regex Tester (replace, flags, groups) →

How this regex works

PartMeaning
(?:https?:\/\/)?(?:www\.|m\.)?optional scheme and www./m.
youtube\.com\/(?:watch\?(?:.*&)?v=|embed\/|shorts\/)watch, embed or Shorts URLs
|youtu\.be\/or the short youtu.be form
([A-Za-z0-9_-]{11})capture group 1: the 11-character video ID
(?:[?&#].*)?$optional extra parameters such as t=42

Examples

InputResult
https://www.youtube.com/watch?v=dQw4w9WgXcQ✓ Matches
https://youtu.be/dQw4w9WgXcQ?t=42✓ Matches
https://www.youtube.com/shorts/dQw4w9WgXcQ✓ Matches
https://www.youtube.com/watch?list=PL1&v=dQw4w9WgXcQ✓ Matches
youtube.com/embed/dQw4w9WgXcQ✓ Matches
https://vimeo.com/123456✗ No match
https://www.youtube.com/watch?v=short✗ No match
https://www.youtube.com/channel/UC123✗ No match

Every example above is checked automatically in JavaScript and Python each time the site is built.

Limitations and edge cases

Read the video ID from capture group 1. YouTube IDs are currently 11 characters, but this is a convention, not a published guarantee.

When to use this pattern

Turning pasted YouTube links into embeds or thumbnails (https://img.youtube.com/vi/ID/hqdefault.jpg).

Use it in your language

JavaScript

const re = /^(?:https?:\/\/)?(?:www\.|m\.)?(?:youtube\.com\/(?:watch\?(?:.*&)?v=|embed\/|shorts\/)|youtu\.be\/)([A-Za-z0-9_-]{11})(?:[?&#].*)?$/;
re.test(input); // true or false

Python

import re
pattern = re.compile(r'^(?:https?:\/\/)?(?:www\.|m\.)?(?:youtube\.com\/(?:watch\?(?:.*&)?v=|embed\/|shorts\/)|youtu\.be\/)([A-Za-z0-9_-]{11})(?:[?&#].*)?$')
bool(pattern.search(value))

PHP

$ok = preg_match('~^(?:https?:\\/\\/)?(?:www\\.|m\\.)?(?:youtube\\.com\\/(?:watch\\?(?:.*&)?v=|embed\\/|shorts\\/)|youtu\\.be\\/)([A-Za-z0-9_-]{11})(?:[?&#].*)?$~', $value) === 1;

Java

Pattern pattern = Pattern.compile("^(?:https?:\\/\\/)?(?:www\\.|m\\.)?(?:youtube\\.com\\/(?:watch\\?(?:.*&)?v=|embed\\/|shorts\\/)|youtu\\.be\\/)([A-Za-z0-9_-]{11})(?:[?&#].*)?$");
boolean ok = pattern.matcher(value).find();

Go

re := regexp.MustCompile(`^(?:https?:\/\/)?(?:www\.|m\.)?(?:youtube\.com\/(?:watch\?(?:.*&)?v=|embed\/|shorts\/)|youtu\.be\/)([A-Za-z0-9_-]{11})(?:[?&#].*)?$`)
ok := re.MatchString(value)

More regex patterns

Frequently asked questions

What is the regex for YouTube video URL?
See the pattern at the top of this page. Read the video ID from capture group 1. YouTube IDs are currently 11 characters, but this is a convention, not a published guarantee.
Which languages does this pattern work in?
It is tested in JavaScript and Python and uses syntax that also works in PHP (PCRE) and Java. It also works in Go’s RE2 engine.
Why does the pattern start with ^ and end with $?
They anchor the match to the whole string, so "abchttps://www.youtube.com/watch?v=dQw4w9WgXcQxyz" cannot pass just because it contains a valid part. To search for matches inside longer text, remove ^ and $ (use \b where word boundaries help) and add the g flag.