Skip to content

Instantly share code, notes, and snippets.

@vexdy
Created April 6, 2021 14:27
Show Gist options
  • Save vexdy/5c0d4cac66462a7c5201152fe427847a to your computer and use it in GitHub Desktop.
Save vexdy/5c0d4cac66462a7c5201152fe427847a to your computer and use it in GitHub Desktop.
Find youtube channel ID with regex in javascript
const fetch = require('node-fetch');
const regex = /<link rel="canonical" href="(https?:\/\/)?(www\.)?youtube\.com\/(channel|user)\/[\w-]+">/g
fetch('https://www.youtube.com/channel/UCa10nxShhzNrCE1o2ZOPztg')
.then(res => res.text())
.then(body => console.log(body.match(regex)[0].split(" ")[2].replace('href=\"', '').replace('\">', '').split("/")[4]))
// Expected output:
// UCa10nxShhzNrCE1o2ZOPztg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment