youtube-video-id
Version:
Extracts the YouTube video ID from a URL or string.
38 lines (34 loc) • 1.25 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.YouTubeVideoId = factory());
})(this, (function () { 'use strict';
/**
* 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;
}
return YouTubeVideoId;
}));
//# sourceMappingURL=youtube-video-id.js.map