generate functionTue, 13 Dec 2022

js replace all youtube urls in string with embed code

function replace_urls(text) { const youtube_urls = text.match(/(http(s)?:\/\/)?(www\.)?youtube\.com\/watch\?v=([a-zA-Z0-9-]{11})/g); return youtube_urls.map(url => { return url.replace(url, `<iframe width="560" height="315" src="${url.replace(/(http(s)?:\/\/)?(www\.)?youtu\.be\//, "https://www.youtube.com/embed/")}" frameborder="0" allowfullscreen></iframe>`) }).join("") }

Javascript
Generate More

Questions about programming?Chat with your personal AI assistant