REGEX - GET youtube video ID & PLAYLIST ID by URL
GET youtube video ID & PLAYLIST ID by URL
by bvotcode
HTML
<!--
more info http://stackoverflow.com/questions/3452546/javascript-regex-how-to-get-youtube-video-id-from-url
-->
JavaScript
//https://www.youtube.com/watch?v=QH2-TGUlwu4&feature=youtu.be
//url = "https://youtu.be/QH2-TGUlwu4"
//url = window.location.href ;
//alert(url);
url = '<div class="col_three_fifth"> <div> <iframe width="560" height="315" src="https://www.youtube.com/embed/Xo4lTJc_c1c" title="YouTube video player?playsinline=1" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> </div> <div class="row"> <div class="col-sm-12" style="padding-top:30px; text-align: center;"> <div class="addthis_inline_share_toolbox_u9ci"></div> </div> </div> </div>'
REGEX = /(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|playlist\?|watch\?v=|watch\?.+(?:&|&);v=))([a-zA-Z0-9\-_]{11})?(?:(?:\?|&|&)index=((?:\d){1,3}))?(?:(?:\?|&|&)?list=([a-zA-Z\-_0-9]{34}))?(?:\S+)?.*/
alert(url.match(REGEX)[2]);