Skip to content

Instantly share code, notes, and snippets.

@nanodracula
Created January 22, 2019 09:59
Show Gist options
  • Select an option

  • Save nanodracula/8ae097f70ce3ba3d513fd6a77fa384bc to your computer and use it in GitHub Desktop.

Select an option

Save nanodracula/8ae097f70ce3ba3d513fd6a77fa384bc to your computer and use it in GitHub Desktop.
/**
* Convert URL from Youtube video for URL for embeding.
*
* @param {string} videoUrl - URL of Youtube VIdeo
*/
function makeYoutubeEmbed(videoUrl) {
if (typeof videoUrl !== 'string') {
return undefined
}
const getId = (url) => {
const regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/
const match = url.match(regExp)
if (match && match[2].length == 11) {
return match[2]
}
}
const id = getId(videoUrl)
if (id) {
return '//www.youtube.com/embed/' + id
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment