generate functionThu, 08 Dec 2022

detect youtube link in text and replace it with embed

function youtubeEmbed(text) { let youtubeReg = /(https?:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/.+/gi; let youtubeMatch = text.match(youtubeReg); if (youtubeMatch) { let youtubeEmbed = youtubeMatch[0].replace("watch?v=", "embed/"); text = text.replace(youtubeMatch[0], `<iframe width="420" height="315" src="${youtubeEmbed}"></iframe>`); } return text; }

Javascript
Generate More

Questions about programming?Chat with your personal AI assistant