YouTube URL Regex
Matches standard, short (youtu.be), embed, and Shorts YouTube video URLs and validates the 11-character video ID.
Regex Pattern
^(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:watch\?v=|embed\/|shorts\/)|youtu\.be\/)[\w-]{11}(?:\S*)?$Pattern Breakdown
Hover over a token to see what it does.
| Token | Meaning |
|---|---|
| (?:https?:\/\/)? | Optional http:// or https:// scheme |
| (?:www\.)? | Optional www. subdomain prefix |
| youtube\.com\/(?:watch\?v=|embed\/|shorts\/) | youtube.com host followed by one of the watch, embed, or shorts path prefixes |
| youtu\.be\/ | Alternative shortened youtu.be host followed by a slash |
| [\w-]{11} | Exactly 11 word or hyphen characters, the length of a YouTube video ID |
| (?:\S*)? | Optional trailing non-whitespace characters such as extra query parameters |
| $ | Anchors the match to the end of the string |
Detailed Explanation
What it does
This pattern recognizes the several URL shapes YouTube uses for a single video: the full watch URL, the shortened youtu.be link, embed URLs, and Shorts URLs, and verifies that the video ID portion is exactly 11 characters, matching YouTube's actual ID format.
Why it works
An outer alternation between the youtube.com path prefixes and the youtu.be short-link host covers every common sharing format, while the optional scheme and www. groups let the pattern accept URLs pasted with or without a protocol. The {11} quantifier on [\w-] enforces YouTube's fixed video ID length, which rejects both truncated IDs and non-video YouTube links like channel or playlist URLs that don't fit this exact shape.
Common use cases
- Validating a YouTube link field in a content management system before embedding it
- Extracting and normalizing YouTube video IDs from user-submitted URLs of varying formats
- Filtering a list of pasted links to only those that are playable YouTube videos
- Building a lightweight oEmbed-style preview generator that needs a confirmed video ID
Edge cases
- Shortened youtu.be links, e.g. https://youtu.be/dQw4w9WgXcQ, are matched just like full watch URLs
- URLs without a scheme, like youtube.com/watch?v=dQw4w9WgXcQ, are matched because the scheme group is optional
- Extra query parameters after the ID, such as &t=30s, are tolerated by the trailing (?:\S*)? group
- Video IDs shorter than 11 characters are rejected since the length is fixed rather than a range
- Playlist-only URLs (youtube.com/playlist?list=...) and channel URLs are intentionally rejected since they don't match any of the three recognized prefixes
Limitations
- Does not validate that the 11-character ID actually corresponds to an existing, public video
- Does not extract or validate playlist IDs when a video URL also includes a &list= parameter
- Mobile app deep links (e.g. youtube://) are not recognized, only standard web URLs
- Does not handle YouTube Music (music.youtube.com) URLs, which use a different host
Interactive Tester
Edit the pattern or text below — matching runs live in your browser.
Test Cases
Editable — add your own inputs to see if they pass.
| Input | Expected | Result | |
|---|---|---|---|
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass |
Language Variants
Production-ready examples in 12 languages.
const youtubeUrlRegex = /^(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:watch\?v=|embed\/|shorts\/)|youtu\.be\/)[\w-]{11}(?:\S*)?$/;
console.log(youtubeUrlRegex.test('https://youtu.be/dQw4w9WgXcQ')); // trueCommon Mistakes
Using .+ instead of [\w-]{11} for the video ID, which accepts malformed IDs of any length
Fix: Constrain the ID to exactly 11 word/hyphen characters to match YouTube's actual ID format
Only matching the youtube.com/watch?v= form and rejecting valid youtu.be short links
Fix: Include an alternation for the youtu.be host alongside the full youtube.com prefixes
Requiring the https:// scheme, which rejects URLs pasted without a protocol
Fix: Make the scheme group optional with (?:https?://)? if bare-domain input is expected
Performance Notes
- The outer alternation between youtube.com and youtu.be is checked once near the start, so there's minimal backtracking overhead
- The fixed-length quantifier {11} on the video ID is O(1) to check compared to an unbounded + or *
- Anchoring with ^ and $ avoids scanning the entire input for a match when validating a single URL string
Browser Compatibility
| Engine | Supported | Notes |
|---|---|---|
| Chrome | Yes | — |
| Firefox | Yes | — |
| Safari | Yes | — |
| Edge | Yes | — |
| Node.js | Yes | — |