2010-11-08 96 views
0

我有一個頁面上的YouTube視頻列表,我想用JS來獲取每個<embed>標籤的src URL列表,並使用它們在頁面的其他地方添加縮略圖圖像。爲什麼這個JS RegExp不能工作?

要做到這一點,我需要使用正則表達式從YouTube網址抓取視頻ID,但它拒絕工作,即使正則表達式出現在工作的時候我在這裏測試:http://www.regular-expressions.info/javascriptexample.html

下面的代碼我有:

**這裏是JSBin頁面看到這一切在行動:每次推動整個MYSRC串時間http://jsbin.com/uvoya3/23/edit

var addImages = function() { 
    var features = document.getElementById('features'), 
    embeds = features.getElementsByTagName('embed'), 
    ids = [], i, thumbNav, items, mysrc, pattern, ytid, newImg, matchArray; 

    for (i = 0; i < embeds.length; i += 1) { 
    mysrc = embeds[i].getAttribute('src'); 
    pattern = /^(http:\/\/www.youtube.com\/v\/)([a-zA-Z0-9]*)(\?[^\?]*)$/; 
    ytid = mysrc.replace(pattern, '$2'); 

    alert("src number " + i + " is " + ytid); 
    ids.push(ytid); 
    } 

}; 

window.onload = addImages; 

警報是有測試什麼的正則表達式的查找和因爲它不是matchi一點都不。該MYSRC值

http://www.youtube.com/v/jfiNQGFVjb4?fs=1&amp;hl=en_US 
http://www.youtube.com/v/qtzjzMsJiO8?fs=1&amp;hl=en_US 
http://www.youtube.com/v/baa-dGj2LhQ?fs=1&amp;hl=en_US 

它正在從這個HTML拉

<ul id="features"> 
    <li><object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/jfiNQGFVjb4?fs=1&amp;hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/jfiNQGFVjb4?fs=1&amp;hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object></li> 
    <li><object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/qtzjzMsJiO8?fs=1&amp;hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/qtzjzMsJiO8?fs=1&amp;hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object></li> 
    <li><object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/baa-dGj2LhQ?fs=1&amp;hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/baa-dGj2LhQ?fs=1&amp;hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object></li> 
</ul> 

有誰看到爲什麼我的正則表達式或我的JS是偏離了軌道嗎?

** PS這裏是JSBin URL http://jsbin.com/uvoya3/23/edit

+0

看來,據我可以告訴是工作得很好,儘管你需要確保「src」的名字可以包含短劃線。 – Pointy 2010-11-08 19:57:06

回答

0

它的正常工作,除了第三個,因爲一個包含-。順便說一下,_也可能受支持。

因此,更好的正則表達式是:/^(http:\/\/www.youtube.com\/v\/)([a-zA-Z0-9-_]*)(\?[^\?]*)$/

在我的JavaScript控制檯能正常工作:

> "http://www.youtube.com/v/baa-dGj2LhQ?fs=1&amp;hl=en_US".replace(/^(http:\/\/www.youtube.com\/v\/)([a-zA-Z0-9-_]*)(\?[^\?]*)$/, '$2'); 
< "baa-dGj2LhQ" 

您可以通過該方式優化代碼:

var addImages = function() { 

    // Part 1 
    var features = document.getElementById('features'), 
    embeds = features.getElementsByTagName('embed'), 
    pattern = /^(http:\/\/www.youtube.com\/v\/)([a-zA-Z0-9-_]*)(\?[^\?]*)$/, 
    ids = [], i, thumbNav, items, mysrc, ytid, newImg; 

    for (i = 0; i < embeds.length; i++) { 
    ids[i] = embeds[i].src.replace(pattern, '$2'); 
    } 

    ...