codingtcher.com
Theme Night
Home
Login

Get youtube video id from url javascript

Home » Tutorial Post » Get youtube video id from url javascript
<script>

function getytId(url){
    if (url.match('youtu.be/')){
        var final_ID = url.match(/(https:|http:|)(\/\/www\.|\/\/|)(.*?)\/(.{11})/i);
		return final_ID[4];
	}
    else{
	 var IDD = url.match(/(https:|http:|):(\/\/www\.|\/\/|)(.*?)\/(embed\/|watch.*?v=|)([a-z_A-Z0-9\-]{11})/i);
	 return IDD[5];
	}
}

document.write(getytId("https://youtu.be/zUaL-m2u2Cg"));
document.write("<br>");
document.write(getytId("https://www.youtube.com/watch?v=RuSPwJSvOyI"));
document.write("<br>");
document.write(getytId("https://www.youtube.com/embed/oQ7n_C3QtIk"));

</script>

Related Post