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<</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
Post a Comment