28 lines
780 B
TypeScript
28 lines
780 B
TypeScript
export function extractYoutubeId(input: string): string {
|
|
const trimmed = input.trim()
|
|
if (!trimmed) return ''
|
|
|
|
// Direct 11-char ID
|
|
if (/^[a-zA-Z0-9_-]{11}$/.test(trimmed)) {
|
|
return trimmed
|
|
}
|
|
|
|
// youtu.be/ID
|
|
const shortMatch = trimmed.match(/youtu\.be\/([a-zA-Z0-9_-]{11})/)
|
|
if (shortMatch) return shortMatch[1]
|
|
|
|
// youtube.com/watch?v=ID
|
|
const watchMatch = trimmed.match(/[?&]v=([a-zA-Z0-9_-]{11})/)
|
|
if (watchMatch) return watchMatch[1]
|
|
|
|
// youtube.com/embed/ID
|
|
const embedMatch = trimmed.match(/youtube\.com\/embed\/([a-zA-Z0-9_-]{11})/)
|
|
if (embedMatch) return embedMatch[1]
|
|
|
|
// youtube.com/shorts/ID
|
|
const shortsMatch = trimmed.match(/youtube\.com\/shorts\/([a-zA-Z0-9_-]{11})/)
|
|
if (shortsMatch) return shortsMatch[1]
|
|
|
|
return trimmed
|
|
}
|