Regex Tester & Debugger→Specialized Version
šŸ”

YouTube URL Regex Tester

Test and explain the youtube url tester pattern

//g
Flags:
Examples:
https://youtu.be/dQw4w9WgXcQ https://www.youtube.com/watch?v=dQw4w9WgXcQ&t=30 https://www.youtube.com/@channelname https://vimeo.com/123456789
#MatchIndexGroups
1youtu.be/dQw4w9WgXcQ8dQw4w9WgXcQ
2youtube.com/watch?v=dQw4w9WgXcQ41dQw4w9WgXcQ

The Pattern

``regex (?:youtu\.be\/|youtube\.com\/(?:watch\?v=|embed\/|shorts\/))([A-Za-z0-9_-]{11}) `

Flags: g

What It Accepts and Rejects

InputResult
https://youtu.be/dQw4w9WgXcQāœ… matches
https://www.youtube.com/watch?v=dQw4w9WgXcQ&t=30āœ… matches
https://www.youtube.com/@channelnameāŒ rejected
https://vimeo.com/123456789āŒ rejected

One Capture, Four URL Shapes

The alternation covers the four forms YouTube actually serves: the youtu.be short link, the standard watch?v= URL, the embed/ iframe source and the shorts/ path. Each leads to the same capture group.

The ID class [A-Za-z0-9_-]{11} is exact: YouTube video IDs are always eleven characters from that set. Pinning the length is what stops the pattern from swallowing the &t=30 that follows in a URL with parameters.

The Limits of This Pattern

The pattern extracts an ID, which is not the same as confirming a video exists, is public, or is embeddable. Playlist URLs, channel URLs and live stream links have different shapes and either fail to match or produce a wrong capture.

If you are embedding user-supplied links, extract the ID with this and then build your own embed URL from it. Never pass a user-supplied URL straight into an iframe src.

Testing Before Shipping

A regex that has only been tried against inputs you expect to match is untested. Every pattern needs three kinds of case:

1. Valid inputs that should match, including the awkward-but-legal ones. 2. Invalid inputs that should not, especially near-misses that differ by one character. 3. Adversarial inputs — very long strings, unusual Unicode, and nesting that could trigger catastrophic backtracking.

Paste your own examples into the tester above and watch which lines highlight. A pattern that matches everything you throw at it is usually too permissive rather than correct.

Catastrophic Backtracking

Nested quantifiers over overlapping character classes — (a+)+, (\w+\s?)* — can take exponential time on a non-matching input. On a server that is a denial-of-service bug, not a performance issue. If a pattern is applied to user input, bound the input length first and prefer explicit alternation over nested repetition.

Anchors, Greediness and Backtracking

Three behaviours account for most regex surprises, and this pattern shows all three.

Anchors. ^ and $ pin the match to the start and end of the input. Without them, \d{3} matches the 123 inside abc123def. With m in the flags — as here — they pin to each *line* instead, which is what lets one pattern be tested against a list.

Greediness. .* takes as much as it can and gives back only when forced; .*? takes as little as possible. On , the pattern <.*> matches the whole string and <.*?> matches just .

Backtracking. When a match fails, the engine reverses and tries other splits. Nested quantifiers like (a+)+ make that exponential, and a 30-character input can hang a server — a class of denial of service known as ReDoS. Avoid nesting quantifiers, and prefer explicit character classes over . wherever you can.

Testing It Properly

`javascript const pattern = /(?:youtu\.be\/|youtube\.com\/(?:watch\?v=|embed\/|shorts\/))([A-Za-z0-9_-]{11})/g;

// A global regex keeps lastIndex between calls, so reusing one across // test() calls returns alternating true/false on the same input. pattern.lastIndex = 0;

// Named groups make the result readable const named = /(?\d{4})-(?\d{2})/; const { groups } = '2026-08'.match(named); `

Write the failing cases first. A pattern that accepts everything valid is easy; one that also rejects everything invalid is the hard half, and it is where the bugs are.

When Not to Use a Regex

Structured formats have parsers, and the parser is always more correct: new URL() for URLs, DOMParser for HTML, JSON.parse` for JSON, a date library for dates. Reach for a regex to *find* things in unstructured text, not to validate something a parser understands.

Frequently Asked Questions

Does this pattern handle every valid case?

The pattern extracts an ID, which is not the same as confirming a video exists, is public, or is embeddable. Playlist URLs, channel URLs and live stream links have different shapes and either fail to match or produce a wrong capture. If you are embedding user-supplied links, extract the ID with this and then build your own embed URL from it. Never pass a user-supplied URL straight into an iframe

Why does the pattern use the flags it uses?

Flags `g`. The `g` flag finds every match rather than stopping at the first. Changing them changes the results, so test with the flags you will ship.

Is validating this with a regex the right approach?

For a format check before doing real work, usually yes. For anything security-critical, a regex confirms shape and nothing else — parse the value with a real parser, or verify it against the system that owns it, before trusting it.

Related Tools

Explore other tools you might find useful:

More Regex Tester & Debugger tools

You might also need