我想要得到v=id从YouTube的URL与JavaScript(没有jQuery,纯JavaScript)。
YouTube URL格式示例
http://www.youtube.com/watch?v=u8nQa1cJyX8&a=GxdCwVVULXctT2lYDEPllDR0LRTutYfW
http://www.youtube.com/watch?v=u8nQa1cJyX8
或在URL中包含视频ID的任何其他YouTube格式。
这些格式的结果
u8nQa1cJyX8
我想要得到v=id从YouTube的URL与JavaScript(没有jQuery,纯JavaScript)。
YouTube URL格式示例
http://www.youtube.com/watch?v=u8nQa1cJyX8&a=GxdCwVVULXctT2lYDEPllDR0LRTutYfW
http://www.youtube.com/watch?v=u8nQa1cJyX8
或在URL中包含视频ID的任何其他YouTube格式。
这些格式的结果
u8nQa1cJyX8
当前回答
如果有人需要Kotlin中的完美函数来节省他们的时间。希望这能有所帮助
fun extractYTId(ytUrl: String?): String? {
var vId: String? = null
val pattern = Pattern.compile(
"^https?://.*(?:youtu.be/|v/|u/\\w/|embed/|watch?v=)([^#&?]*).*$",
Pattern.CASE_INSENSITIVE
)
val matcher = pattern.matcher(ytUrl)
if (matcher.matches()) {
vId = matcher.group(1)
}
return vId
}
其他回答
我在下面写了一个函数:
function getYoutubeUrlId (url) {
const urlObject = new URL(url);
let urlOrigin = urlObject.origin;
let urlPath = urlObject.pathname;
if (urlOrigin.search('youtu.be') > -1) {
return urlPath.substr(1);
}
if (urlPath.search('embed') > -1) {
// Örneğin "/embed/wCCSEol8oSc" ise "wCCSEol8oSc" return eder.
return urlPath.substr(7);
}
return urlObject.searchParams.get('v');
},
https://gist.github.com/semihkeskindev/8a4339c27203c5fabaf2824308c7868f
正如webstrap在评论中提到的:
如果视频以“v”开头,并且它来自youtube .be,它就可以工作 正则表达式包含一个小错误\??v?=?这应该在 注意部分,否则如果id以a开头,您将过滤'v' “v”。这应该能解决问题 / ^。* ((youtu.be \ /) | (v \ /) | (\ w / u \ / \ \ /) |(嵌入\ /)|(看\ ? ? v ?=?))([^#\&\?]*).*/
/^.*(youtu.be\/|v\/|e\/|u\/\w+\/|embed\/|v=)([^#\&\?]*).*/
测试:
http://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0 http://www.youtube.com/embed/0zM3nApSvMg?rel=0 http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index http://www.youtube.com/watch?v=0zM3nApSvMg http://youtu.be/0zM3nApSvMg http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/KdwsulMb8EQ http://youtu.be/dQw4w9WgXcQ http://www.youtube.com/embed/dQw4w9WgXcQ http://www.youtube.com/v/dQw4w9WgXcQ http://www.youtube.com/e/dQw4w9WgXcQ http://www.youtube.com/watch?v=dQw4w9WgXcQ http://www.youtube.com/?v=dQw4w9WgXcQ http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ http://www.youtube.com/?feature=player_embedded&v=dQw4w9WgXcQ http://www.youtube.com/user/IngridMichaelsonVEVO#p/u/11/KdwsulMb8EQ http://www.youtube-nocookie.com/v/6L3ZvIMwZFM?version=3&hl=en_US&rel=0
受到另一个答案的启发。
回答很好,但我最近发现,如果你试图在文本中找到你的youtube链接,并在youtube url后放置一些随机文本,regexp匹配的方式比需要的更多。改进的Chris Nolet回答:
- ^ * (?): youtu。be | u - v | \ \ \ w / |嵌入- | watch \ v ? =)([^#\&\?]{ 11,11})。* /
铊;博士。
匹配这个问题上的所有URL示例。
let re = /(https?:\/\/)?(((m|www)\.)?(youtube(-nocookie)?|youtube.googleapis)\.com.*(v\/|v=|vi=|vi\/|e\/|embed\/|user\/.*\/u\/\d+\/)|youtu\.be\/)([_0-9a-z-]+)/i;
let id = "https://www.youtube.com/watch?v=l-gQLqv9f4o".match(re)[7];
ID总是在匹配组8中。
我从这个问题的答案中抓取的所有url的实例: https://regexr.com/3u0d4
完整的说明:
正如许多回答/评论所提出的,youtube视频url有很多种格式。甚至是多个顶级域名,它们可以显示为“托管”。
您可以通过上面的regexr链接查看我检查过的变体的完整列表。
让我们分析一下RegExp。
^将字符串锁定到字符串的开始。 (https ?: \ \ /) ?可选协议http://或https:// The ?使前一项可选,因此s和整个组(括号中包含的任何内容)都是可选的。
好了,接下来的部分是最重要的部分。基本上我们有两个选项,不同版本的[optional-subdomain].youtube.com/...[id]和缩短的链接youu。/ (id)版本。
( // Start a group which will match everything after the protocol and up to just before the video id.
((m|www)\.)? // Optional subdomain, this supports looking for 'm' or 'www'.
(youtube(-nocookie)?|youtube.googleapis) // There are three domains where youtube videos can be accessed. This matches them.
\.com // The .com at the end of the domain.
.* // Match anything
(v\/|v=|vi=|vi\/|e\/|embed\/|user\/.*\/u\/\d+\/) // These are all the things that can come right before the video id. The | character means OR so the first one in the "list" matches.
| // There is one more domain where you can get to youtube, it's the link shortening url which is just followed by the video id. This OR separates all the stuff in this group and the link shortening url.
youtu\.be\/ // The link shortening domain
) // End of group
最后,我们有组来选择视频ID。至少一个数字、字母、下划线或破折号字符。
([_0-9a-z-]+)
通过浏览regexr链接,并查看表达式的每个部分如何与url中的文本匹配,您可以找到关于regex每个部分的更多细节。