youtube-video-id
Version:
Extracts the YouTube video ID from a URL or string.
30 lines • 867 B
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = YouTubeVideoId;
/**
* YouTube URL regex.
*
* Patterns:
* https://www.youtube.com/watch?v=<ID>
* https://www.youtube.com/watch?v=<ID>&feature=youtu.be
* https://youtu.be/<ID>
* https://youtu.be/<ID>?t=1s
*/
var regex = /(youtube\.com\/watch\?v=|youtu\.be\/)([0-9A-Za-z_-]{10}[048AEIMQUYcgkosw])/;
/**
* Get the YouTube video ID from a URL or string.
*
* @param url - The URL or string.
* @returns - The video ID.
*/
function YouTubeVideoId(url) {
if (typeof url !== 'string') {
throw new TypeError('First argument must be a string');
}
var match = url.match(regex);
if ((match === null || match === void 0 ? void 0 : match.length) && match[2]) {
return match[2];
}
return url;
}
//# sourceMappingURL=index.js.map
;