Skip to content

Instantly share code, notes, and snippets.

@izszzz
Last active August 15, 2021 14:52
Show Gist options
  • Save izszzz/9c15ba33a7c280803d3ef5d14f19766a to your computer and use it in GitHub Desktop.
Save izszzz/9c15ba33a7c280803d3ef5d14f19766a to your computer and use it in GitHub Desktop.
getIDfromYoutubeURL
// https://www.youtube.com/watch?v=:ID
// https://www.youtube.com/channel/:ID
const getIDfromYoutubeURL = (url: string): string | null => {
const newURL = new URL(url);
const { pathname } = newURL;
if (pathname.includes("watch")) return newURL.searchParams.get("v");
if (pathname.includes("channel")) return pathname.split("/")[2];
return null;
};
export default getIDfromYoutubeURL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment