Social
YouTube video URL Regex
YouTube shares the same video through several URL forms: the classic watch?v= link, the youtu.be short link, the shorts path and the embed path used in iframes. This pattern recognises all four, with or without https and with optional www or m subdomains, and then requires the 11 character video ID made of letters, digits, dashes and underscores. Extra query parameters such as a timestamp are allowed after the ID. It is a solid choice for embed widgets, link previews and comment filters. It does not handle watch URLs where v= is not the first parameter, and it cannot tell whether the video is public.
Pattern (JavaScript)
/^(?:https?://)?(?:www\.|m\.)?(?:youtube\.com/(?:watch\?v=|shorts/|embed/)|youtu\.be/)[A-Za-z0-9_-]{11}(?:[?&#]\S*)?$/Starts at the start of the line/string, then (the text "http", then the character "s", optionally (zero or one time), then the text "://"), optionally (zero or one time), then (the text "www." OR the text "m."), optionally (zero or one time), then (the text "youtube.com/", then (the text "watch?v=" OR the text "shorts/" OR the text "embed/") OR the text "youtu.be/"), then one of the characters A-Za-z0-9_-, exactly 11 times, then (one of the characters ?&#, then any character that is not whitespace, zero or more times), optionally (zero or one time), then the end of the line/string.
Examples
Matches
- ✓ https://www.youtube.com/watch?v=AbCdEfGhIjK
- ✓ https://youtu.be/Test_Video1
- ✓ youtube.com/shorts/x1y2z3a4b5c?feature=share
- ✓ https://www.youtube.com/embed/AbCdEfGhIjK
Does not match
- ✗ https://www.youtube.com/watch?v=short
- ✗ https://vimeo.com/123456789
- ✗ https://www.youtube.com/@test_channel
- ✗ https://youtu.be/
Try it
0 matches (JavaScript engine)
https://www.youtube.com/watch?v=AbCdEfGhIjK
https://youtu.be/Test_Video1
youtube.com/shorts/x1y2z3a4b5c?feature=share
https://www.youtube.com/embed/AbCdEfGhIjK
https://www.youtube.com/watch?v=short
https://vimeo.com/123456789
https://www.youtube.com/@test_channel
https://youtu.be/Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?:https?:\/\/)?(?:www\.|m\.)?(?:youtube\.com\/(?:watch\?v=|shorts\/|embed\/)|youtu\.be\/)[A-Za-z0-9_-]{11}(?:[?&#]\S*)?$/g;
const text = "https://www.youtube.com/watch?v=AbCdEfGhIjK";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(?:https?://)?(?:www\.|m\.)?(?:youtube\.com/(?:watch\?v=|shorts/|embed/)|youtu\.be/)[A-Za-z0-9_-]{11}(?:[?&#]\S*)?$')
text = 'https://www.youtube.com/watch?v=AbCdEfGhIjK'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '~^(?:https?://)?(?:www\.|m\.)?(?:youtube\.com/(?:watch\?v=|shorts/|embed/)|youtu\.be/)[A-Za-z0-9_-]{11}(?:[?&#]\S*)?$~';
$text = 'https://www.youtube.com/watch?v=AbCdEfGhIjK';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^(?:https?://)?(?:www\\.|m\\.)?(?:youtube\\.com/(?:watch\\?v=|shorts/|embed/)|youtu\\.be/)[A-Za-z0-9_-]{11}(?:[?&#]\\S*)?$");
String text = "https://www.youtube.com/watch?v=AbCdEfGhIjK";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(?:https?://)?(?:www\.|m\.)?(?:youtube\.com/(?:watch\?v=|shorts/|embed/)|youtu\.be/)[A-Za-z0-9_-]{11}(?:[?&#]\S*)?$`)
text := "https://www.youtube.com/watch?v=AbCdEfGhIjK"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?:https?://)?(?:www\.|m\.)?(?:youtube\.com/(?:watch\?v=|shorts/|embed/)|youtu\.be/)[A-Za-z0-9_-]{11}(?:[?&#]\S*)?$");
var text = "https://www.youtube.com/watch?v=AbCdEfGhIjK";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^(?:https?://)?(?:www\.|m\.)?(?:youtube\.com/(?:watch\?v=|shorts/|embed/)|youtu\.be/)[A-Za-z0-9_-]{11}(?:[?&#]\S*)?$").unwrap();
let text = "https://www.youtube.com/watch?v=AbCdEfGhIjK";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(?:https?:\/\/)?(?:www\.|m\.)?(?:youtube\.com\/(?:watch\?v=|shorts\/|embed\/)|youtu\.be\/)[A-Za-z0-9_-]{11}(?:[?&#]\S*)?$/
text = 'https://www.youtube.com/watch?v=AbCdEfGhIjK'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How do I extract just the video ID?
- Wrap [A-Za-z0-9_-]{11} in a capture group and read that group after a match. The ID is the same across all four URL forms, so one group works for every variant.
- Why is the video ID exactly 11 characters?
- YouTube video IDs have been 11 characters of a URL-safe Base64 alphabet since the site launched. Enforcing that length rejects truncated or mangled links.
- What about links like watch?feature=share&v=ID?
- This pattern expects v= right after watch?, which covers the vast majority of shared links. For full coverage, parse the URL with the URL API and read the v search parameter instead.
Related patterns
- YouTube channel URL^(?:https?://)?(?:www\.)?youtube\.com/(?:@[A-Za-z0-9._-]{3,30}|channel/UC[A-Za-z0-9_-]{22}|c/[A-Za-z0-9_-]+)/?$
- Discord invite link^(?:https?://)?(?:www\.)?(?:discord\.gg|discord(?:app)?\.com/invite)/[A-Za-z0-9-]{2,32}/?$
- Facebook page URL^(?:https?://)?(?:www\.|m\.)?(?:facebook|fb)\.com/[A-Za-z0-9.]{5,50}/?$
- GitHub repository URL^(?:https?://)?(?:www\.)?github\.com/[A-Za-z0-9](?:-?[A-Za-z0-9]){0,38}/[A-Za-z0-9._-]+?(?:\.git)?/?$