首页 > 解决方案 > 从频道 url 中提取 youtube 频道 ID - android

问题描述

示例网址: https: //www.youtube.com/channel/UCAoMPWcQKA_9Af5YhWdrZgw

我只需要 UCAOMPWcQKA_9Af5YhWdrZgw(频道 ID)

标签: androidregexyoutubeyoutube-channels

解决方案


Subhendu Mondal 的决定在这样的链接上崩溃。

// args after id

https://www.youtube.com/channel/UCAoMPWcQKA_9Af5YhWdrZgw?view_as=subscriber 

//minus sign in id

https://www.youtube.com/channel/UCQ18THOcZ8nIP-A3ZCqqZwA

所以这是改进的决定,协议组也从匹配结果中删除

^(?:http|https):\/\/[a-zA-Z-]*\.{0,1}[a-zA-Z-]{3,}\.[a-z]{2,}\/channel\/([a-zA-Z0-9_\-]{3,24})
var str = "https://www.youtube.com/channel/UCAoMPWcQKA_9Af5YhWdrZgw";
var pattern = /^(?:(http|https):\/\/[a-zA-Z-]*\.{0,1}[a-zA-Z-]{3,}\.[a-z]{2,})\/channel\/([a-zA-Z0-9_]{3,})$/;
var match = str.match(pattern);
console.log(match[1]);

更新:

这是最短的方法:

youtube.com\/channel\/([^#\&\?]*).*

推荐阅读