[Javascript] estrarre id link yt

trattorino

Utente Attivo
23 Lug 2016
173
1
18
43
salve, volevochiede se esiste un modo in js di estrarre id di un link youtube es:
ho dei link che sono cosi
Codice:
https://www.youtube.com/watch?v=Czd9eOA919o
https://youtu.be/5FvJ_tir0N0
è possibile estrarre solo id dei diveo?
grazie in anticipo
 
ho risolto così
Codice:
<script>
var url = 'link video;
var videoid = url.match(/(?:https?:\/{2})?(?:w{3}\.)?youtu(?:be)?\.(?:com|be)(?:\/watch\?v=|\/)([^\s&]+)/);
if(videoid != null) {
document.write('<iframe width="320" height="180" src="https://www.youtube.com/embed/',videoid[1],'" frameborder="0" allowfullscreen=""></iframe>');
}
  
</script>
 

Discussioni simili