2011-03-25 232 views
5

我試圖採取一個textarea輸入,它將任何YouTube網址作爲輸入,並像Facebook一樣嵌入視頻。從jQuery的URL獲取YouTube視頻ID

我:

var text = $('#content').val().split(' '); 
    for (i = 0; i < text.length; i++) { 
     var test = text[i].indexOf('youtube.com/watch'); 
     var check = text[i].indexOf('[youtube='); 

     if (test != -1 && check == -1) { 
      var ytid = text[i].substring(text[i].lastIndexOf('=') + 1); 
      text[i] = '[youtube=' + ytid + ']'; 
     } 
    } 
var boxval = text.join(' ').trim(); 

這需要任何YouTube網址,使之成爲一個[youtube=videoid]。然後問題是,當一個URL提交<br>\n在最後它添加]事後。

任何人都知道更好的方法?

+0

你的問題是不完整的,我認爲「那麼我使用.....」 – jimy 2011-03-25 07:49:08

+0

你的邏輯將不適用於像http://www.youtube.com/watch?v=gzDS-Kfd5XQ&feature=youtube_gdata_player這樣的URL ',雖然它是一個合法的YouTube視頻網址。 – 2011-03-25 09:22:37

回答

3

正則表達式剝離救援:

var text = (
"http://www.youtube.com/watch?v=gzDS-Kfd5XQ\n" + 
"http://www.youtube.com/watch?v=gzDS-Kfd5XQ&feature=youtube_gdata_player\n" + 
"http://www.youtube.com/watch?feature=youtube_gdata_player&v=gzDS-Kfd5XQ\n" + 
"http://www.youtube.com/watch?v=gzDS-Kfd5XQ http://www.youtube.com/watch?v=gzDS-Kfd5XQ" 
).split(/\s+/); 
for (var i = 0; i < text.length; i++) { 
    var url = text[i]; 
    if (/^https?\:\/\/.+/i.test(url)) { 
     var temp = /[\?\&]v=([^\?\&]+)/.exec(url); 
     if (temp) { 
      text[i] = '[youtube=' + temp[1] + ']'; 
     } else { 
      text[i] = "URL found but does not contain video id"; 
     } 
    } else { 
     // other case left as an exercise 
    } 
} 
alert(text.join('\n')); 

代碼從here借來的。

0

嘗試使用查詢字符串分析的輔助方法:

var boxval = getParameterByName("v", $('#content').val()); 

助手方法(從here借用):

function getParameterByName(name, url) 
{ 
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); 
    var regexS = "[\\?&]" + name + "=([^&#]*)"; 
    var regex = new RegExp(regexS); 
    var results = regex.exec(url); 
    if(results == null) 
    return ""; 
    else 
    return decodeURIComponent(results[1].replace(/\+/g, " ")); 
} 
3

類似的問題最近被請求(但這個問題需要一個PHP解決方案)。下面是JavaScript版本的my solution to that question

// Linkify youtube URLs which are not already links. 
function linkifyYouTubeURLs(text) { 
    /* Commented regex (in PHP syntax) 
    $text = preg_replace('% 
     # Match any youtube URL in the wild. 
     (?:https?://)? # Optional scheme. Either http or https 
     (?:www\.)?  # Optional www subdomain 
     (?:    # Group host alternatives 
      youtu\.be/  # Either youtu.be, 
     | youtube\.com # or youtube.com 
      (?:   # Group path alternatives 
      /embed/  # Either /embed/ 
      | /v/   # or /v/ 
      | /watch\?v= # or /watch\?v= 
     )    # End path alternatives. 
     )    # End host alternatives. 
     ([\w\-]{10,12}) # $1: Allow 10-12 for 11 char youtube id. 
     \b    # Anchor end to word boundary. 
     [?=&\w]*   # Consume any URL (query) remainder. 
     (?!    # But don\'t match URLs already linked. 
      [\'"][^<>]*> # Not inside a start tag, 
     | </a>   # or <a> element text contents. 
     )    # End negative lookahead assertion. 
     %ix', 
     '<a href="http://www.youtube.com/watch?v=$1">YouTube link: $1</a>', 
     $text); 
    */ 
    // Here is the same regex in JavaScript literal regexp syntax: 
    return text.replace(
     /(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=))([\w\-]{10,12})\b[?=&\w]*(?!['"][^<>]*>|<\/a>)/ig, 
     '<a href="http://www.youtube.com/watch?v=$1">YouTube link: $1</a>'); 
} 

您可以輕鬆地修改替換字符串轉換爲BB代碼,而不是HTML。