generate functionTue, 13 Dec 2022

js replace all youtube urls in string with embed code

function youtubeReplace(str) { let regex = /http(?:s?):\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?‌​[\w\?‌​=]*)?/; let result = str.replace(regex, '<iframe width="560" height="315" src="https://www.youtube.com/embed/$1" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>'); return result; } youtubeReplace("https://www.youtube.com/watch?v=jNQXAC9IVRw")

Javascript
Generate More

Questions about programming?Chat with your personal AI assistant