javascript - Convert youtube url to iframe and remove any surrounding html -


i'm trying use code below convert youtube url embedded iframe code. below working fine, wysiwyg editor adds <p> around plain text url such

<p>https://www.youtube.com/watch?v=mjzhxfsu8uq</p> 

will output

<iframe width="560" height="315" src="https://www.youtube.com/embed/mjzhxfsu9xx&lt</p>" frameborder="0" allowfullscreen=""></iframe>

how can change below removes </p> src?

$('body').html(function(i, html) {     return html.replace(/(?:https:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.+)/g, '<iframe width="560" height="315" src="https://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>'); }); 

how making regex:

/<p>(?:https:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.+)<\/p>/g

debug console test:

var v="<p>https://www.youtube.com/watch?v=mjzhxfsu8uq</p>"; var = v.replace( /<p>(?:https:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.+)<\/p>/g, '<iframe width="560" height="315" src="https://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>' ); console.log(a);

gives: <iframe width="560" height="315" src="https://www.youtube.com/embed/mjzhxfsu8uq" frameborder="0" allowfullscreen></iframe>


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -