generate functionThu, 08 Dec 2022

detect youtube link in text and convert it to embed

function convert(text){ var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/; var match = text.match(regExp); if (match && match[2].length == 11) { return `<iframe width="420" height="315" src="https://www.youtube.com/embed/${match[2]}" frameborder="0" allowfullscreen></iframe>`; } else { return 'no youtube link found'; } }

Javascript
Generate More

Questions about programming?Chat with your personal AI assistant