JSFiddle - React, Tailwind, and code Playground

by Chris Ball

HTML

YouTube ID: <span id="myId"></span>
<br />
<br />
Embed code: <pre id="myCode"></pre>

CSS

#myId {
    color: orange;
}

JavaScript

function getId(url) {
    var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
    var match = url.match(regExp);

    if (match && match[2].length == 11) {
        return match[2];
    } else {
        return 'error';
    }
}

var myId = getId('https://youtu.be/sF5KBMxmHFg?list=RDsF5KBMxmHFg');

$('#myId').html(myId);

$('#myCode').html('<iframe width="560" height="315" src="//www.youtube.com/embed/' + myId + '" frameborder="0" allowfullscreen></iframe>');