JSFiddle - React, Tailwind, and code Playground
by maleol
HTML
<!-- Click the buttons to probe URLs -->
<input type="button" value="https://www.youtube.com/watch?v=p-e2G_VcTms&feature=g-logo&context=G29aead6FOAAAAAAABAA" class="yt-url">
<input type="button" value="https://www.youtube.com/latest" class="yt-url">
JavaScript
function ytVidId(url) {
var p = /^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/;
return (url.match(p)) ? RegExp.$1 : false;
}
// for example snippet only!
document.body.addEventListener('click', function(e) {
if (e.target.className == 'yt-url' && 'undefined' !== e.target.value) {
var ytId = ytVidId(e.target.value);
alert(e.target.value + "\r\nResult: " + (!ytId ? 'false' : ytId));
}
}, false);