YouTube Video ID
Extract 11-character YouTube video IDs from long URLs, short URLs, or embed URLs.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("(?:youtube\\.com\\/watch\\?v=|youtu\\.be\\/|youtube\\.com\\/embed\\/)([\\w\\-]{11})", "g");
const input = "https://www.youtube.com/watch?v=dQw4w9WgXcQ";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
import re
pattern = re.compile(r"(?:youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/embed\/)([\w\-]{11})")
input_text = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?:youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/embed\/)([\w\-]{11})`)
input := `https://www.youtube.com/watch?v=dQw4w9WgXcQ`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
(?:youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/embed\/)([\w\-]{11}) (flags: g)Raw source: (?:youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/embed\/)([\w\-]{11})
How it works
Examples
Input
https://www.youtube.com/watch?v=dQw4w9WgXcQMatches
youtube.com/watch?v=dQw4w9WgXcQ
Input
https://youtu.be/dQw4w9WgXcQMatches
youtu.be/dQw4w9WgXcQ
Input
not a youtube urlNo match
—Common use cases
- •Content scraping and normalisation
- •Embed URL rewriting
- •Analytics tagging and attribution
- •Link preview rendering
Related patterns
CSS Class from `class=""` Attribute
WebExtract the value of a `class="..."` attribute from raw HTML, handling double or single quotes.
GitHub Repository URL
WebMatch GitHub repository URLs and capture the owner and repo segments.
LinkedIn Profile URL
WebMatch LinkedIn profile URLs and capture the profile slug.
Twitter / X URL
WebMatch Twitter/X profile and status URLs, capturing the handle and (optional) tweet ID.