UNPKG

@cch137/format-utils

Version:

A collection of utility modules for formatting and processing data

18 lines (17 loc) 592 B
const ytLinkRegex = /(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:watch\?(?:\S+&)?v=|embed\/|v\/)|youtu\.be\/)([\w-]+)/g; export function extractYouTubeUrls(text) { const matches = text.match(ytLinkRegex); return matches ? matches.filter((link) => link.startsWith("https://") || link.startsWith("http://")) : []; } export function isYouTubeUrl(url) { return Boolean(extractYouTubeUrls(url).length > 0); } export function getYouTubeVideoId(url) { const match = ytLinkRegex.exec(url); if (match !== null) { return match[1]; } return null; }