2017-02-24 92 views
3

我有以下代碼檢測字符串中的視頻ID。 它實際上捕捉到更多的情況下,我需要,我想使它更簡單,在此格式只對字符串返回ID:YouTube視頻ID的正則表達式

https://www.youtube.com/watch?v=85PMSYAguZ8 
https://youtu.be/85PMSYAguZ8 

代碼:

_getVideoIdFromUrl: function (value) { 
var regEx = /^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/; 
var matches = value.match(regEx); 
if (matches) { 
    return matches[1]; 
} 
return false; 

},

我需要幫助來相應地更改regEx,任何想法?

目前檢測:

YES 'http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel', 
YES 'http://www.youtube.com/watch?v=yZ-K7nCVnBI&playnext_from=TL&videos=osPknwzXEas&feature=sub', 
YES 'http://youtu.be/6dwqZw0j_jY', 
YES 'http://www.youtube.com/watch?v=6dwqZw0j_jY&feature=youtu.be',  
YES 'http://www.youtube.com/watch?v=yZ-K7nCVnBI&playnext_from=TL&videos=osPknwzXEas&feature=sub', 
YES 'http://www.youtube.com/embed/nas1rJpm7wY?rel=0', 
YES 'http://www.youtube.com/watch?v=peFZbP64dsU', 
YES 'http://youtube.com/v/dQw4w9WgXcQ?feature=youtube_gdata_player', 
YES 'http://youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player', 
YES 'http://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player', 
YES 'http://youtu.be/afa-5HQHiAs', 
YES 'http://youtu.be/dQw4w9WgXcQ?feature=youtube_gdata_player' 
NO '//www.youtube-nocookie.com/embed/up_lNV-yoK4?rel=0', 
NO 'http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo', 
NO 'http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I', 
NO 'http://www.youtube.com/user/SilkRoadTheatre#p/a/u/2/6dwqZw0j_jY', 
NO 'http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo?rel=0', 
NO 'http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel', 
NO 'http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I', 
NO 'http://youtube.com/vi/dQw4w9WgXcQ?feature=youtube_gdata_player', 
NO 'http://youtube.com/?v=dQw4w9WgXcQ&feature=youtube_gdata_player', 
NO 'http://youtube.com/?vi=dQw4w9WgXcQ&feature=youtube_gdata_player', 
NO 'http://youtube.com/watch?vi=dQw4w9WgXcQ&feature=youtube_gdata_player', 
+0

聽起來是個好主意,請你給我一個簡單的例子,我不是很熟悉的正則表達式。 – Radex

+0

這是一個非常非常基本的正則表達式。 '的https://(:萬維網\ .youtube \的.com /觀看\ V = | youtu \ .BE /?)([\ W-] {11})'。 –

+0

[解決這裏:到目前爲止最好的工作](https://stackoverflow.com/questions/3392993/php-regex-to-get-youtube-video-id/17030234) –

回答

2

試試這個:

urls = ['http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel', 
 
'http://www.youtube.com/watch?v=yZ-K7nCVnBI&playnext_from=TL&videos=osPknwzXEas&feature=sub', 
 
'http://youtu.be/6dwqZw0j_jY', 
 
'http://www.youtube.com/watch?v=6dwqZw0j_jY&feature=youtu.be', 
 
'http://www.youtube.com/watch?v=yZ-K7nCVnBI&playnext_from=TL&videos=osPknwzXEas&feature=sub', 
 
'http://www.youtube.com/embed/nas1rJpm7wY?rel=0', 
 
'http://www.youtube.com/watch?v=peFZbP64dsU', 
 
'http://youtube.com/v/dQw4w9WgXcQ?feature=youtube_gdata_player', 
 
'http://youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player', 
 
'http://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player', 
 
'http://youtu.be/afa-5HQHiAs', 
 
'http://youtu.be/dQw4w9WgXcQ?feature=youtube_gdata_player', 
 
'//www.youtube-nocookie.com/embed/up_lNV-yoK4?rel=0', 
 
'http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo', 
 
'http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I', 
 
'http://www.youtube.com/user/SilkRoadTheatre#p/a/u/2/6dwqZw0j_jY', 
 
'http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo?rel=0', 
 
'http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel', 
 
'http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I', 
 
'http://youtube.com/vi/dQw4w9WgXcQ?feature=youtube_gdata_player', 
 
'http://youtube.com/?v=dQw4w9WgXcQ&feature=youtube_gdata_player', 
 
'http://youtube.com/?vi=dQw4w9WgXcQ&feature=youtube_gdata_player', 
 
'http://youtube.com/watch?vi=dQw4w9WgXcQ&feature=youtube_gdata_player'] 
 

 
_getVideoIdFromUrl = function (value) { 
 
    var regEx = "^(?:https?:)?//[^/]*(?:youtube(?:-nocookie)?\.com|youtu\.be).*[=/]([-\\w]{11})(?:\\?|=|&|$)"; 
 
    var matches = value.match(regEx); 
 
    if (matches) { 
 
     console.log(value + "\n" + matches[1] + "\n"); 
 
    } 
 
    return false; 
 
} 
 

 
urls.forEach(function(url) { 
 
    _getVideoIdFromUrl(url) 
 
});

0

我會做這樣的事情:
它讓你指定 所有實例的視頻ID

const urls = ['http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel', 
 
'http://www.youtube.com/watch?v=yZ-K7nCVnBI&playnext_from=TL&videos=osPknwzXEas&feature=sub', 
 
'http://youtu.be/6dwqZw0j_jY', 
 
'http://www.youtube.com/watch?v=6dwqZw0j_jY&feature=youtu.be', 
 
'http://www.youtube.com/watch?v=yZ-K7nCVnBI&playnext_from=TL&videos=osPknwzXEas&feature=sub', 
 
'http://www.youtube.com/embed/nas1rJpm7wY?rel=0', 
 
'http://www.youtube.com/watch?v=peFZbP64dsU', 
 
'http://youtube.com/v/dQw4w9WgXcQ?feature=youtube_gdata_player', 
 
'http://youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player', 
 
'http://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player', 
 
'http://youtu.be/afa-5HQHiAs', 
 
'http://youtu.be/dQw4w9WgXcQ?feature=youtube_gdata_player', 
 
'//www.youtube-nocookie.com/embed/up_lNV-yoK4?rel=0', 
 
'http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo', 
 
'http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I', 
 
'http://www.youtube.com/user/SilkRoadTheatre#p/a/u/2/6dwqZw0j_jY', 
 
'http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo?rel=0', 
 
'http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel', 
 
'http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I', 
 
'http://youtube.com/vi/dQw4w9WgXcQ?feature=youtube_gdata_player', 
 
'http://youtube.com/?v=dQw4w9WgXcQ&feature=youtube_gdata_player', 
 
'http://youtube.com/?vi=dQw4w9WgXcQ&feature=youtube_gdata_player', 
 
'http://youtube.com/watch?vi=dQw4w9WgXcQ&feature=youtube_gdata_player'] 
 

 
const getVideoId = (url) => { 
 
    if (/^(?:http:\/\/)?youtu\.be/.test(url)){ 
 
    return url.match(/^(?:http:\/\/)?youtu\.be\/(.{11})/)[1] 
 
    } else if (/^(?:(?:http:)?\/\/)?(?:www\.)?youtube(?:-nocookie)?.com/.test(url)){ 
 
    let match = url.match(/(?:http:\/\/)?(?:www\.)?youtube\.com.*?v=(.{11})/) 
 
    if (!match) match = url.match(/(?:vi=|vi?\/|embed\/)(.{11})/) 
 
    if (!match && /user/.test(url)) { 
 
     match = url.split('/') 
 
     return match[match.length - 1].split('?')[0] 
 
    } 
 
    return (match || [,url])[1] 
 
    } else return null 
 
} 
 

 
for (let url of urls){ 
 
    console.log(url) 
 
    console.log(getVideoId(url)) 
 
}

相關問題