Last active
August 15, 2021 14:52
-
-
Save izszzz/9c15ba33a7c280803d3ef5d14f19766a to your computer and use it in GitHub Desktop.
getIDfromYoutubeURL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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