regexhelper
Test a pattern. Copy a ready-made one. No signup.

Regex to Extract a YouTube Video ID

Copy the pattern, paste your YouTube URLs below, and capture the 11-character video ID from each. Below the tester: which URL shapes it covers and where the ID lives.

/ /

What this pattern matches

This expression extracts the 11-character video ID from any common YouTube URL — a full watch?v= link, a short youtu.be link, an embed URL, or a shorts URL — capturing the ID in group one.

How it works, part by part

youtu\.be/ — the short link host, or…

youtube\.com/(?:watch\?v=|embed/|v/|shorts/) — the long host with one of its ID-bearing paths.

([A-Za-z0-9_-]{11}) — the captured video ID: exactly eleven characters from the URL-safe alphabet.

Where the ID lives

Every YouTube video ID is eleven characters of letters, digits, hyphen, and underscore. Because a watch URL can carry extra parameters after the ID (like &t=30s), the fixed length of eleven is what keeps the capture clean. Read the ID from group one of each match.

Use it in your code

Python
import re
text = "https://www.youtube.com/watch?v=dQw4w9WgXcQ https://youtu.be/dQw4w9WgXcQ https://youtube.com/shorts/abc123DEF_-"
pattern = r'''(?:youtu\.be/|youtube\.com/(?:watch\?v=|embed/|v/|shorts/))([A-Za-z0-9_-]{11})'''
for m in re.finditer(pattern, text):
    print(m.group())
JavaScript
const text = "https://www.youtube.com/watch?v=dQw4w9WgXcQ https://youtu.be/dQw4w9WgXcQ https://youtube.com/shorts/abc123DEF_-";
const re = new RegExp(String.raw`(?:youtu\.be/|youtube\.com/(?:watch\?v=|embed/|v/|shorts/))([A-Za-z0-9_-]{11})`, "g");
console.log(text.match(re));

FAQ

Does it handle extra URL parameters?

Yes. Because the ID is fixed at eleven characters, trailing parameters like &t=30s or &list=… are left out of the capture group.

Where is the ID in the match?

In capture group one. The host and path are non-capturing, so group one is always the eleven-character ID.

Does it work for playlist URLs?

No. A bare playlist URL has no video ID. It captures the video ID only when one is present in the path or watch parameter.