2010-09-29 46 views
1

我如何提取src嵌入標籤的屬性與正則表達式?我怎樣才能提取src屬性嵌入標籤

在這個例子中(YouTube視頻):

<div dir="" class="ms-rtestate-field"> 
    <div class="ExternalClass082784C969B644B096E1F293CB4A43C5"> 
     <p> 
      <object width="480" height="385"> 
       <param name="movie" value="http://www.youtube.com/v/Ora35AzLxt0?fs=1&amp;amp;hl=fr_FR"></param> 
       <param name="allowFullScreen" value="true"></param> 
       <param name="allowscriptaccess" value="always"></param> 
       <embed src="http://www.youtube.com/v/Ora35AzLxt0?fs=1&amp;amp;hl=fr_FR" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed> 
      </object> 
     </p> 
    </div> 
</div> 

我只能提取與此正則表達式的完整標籤:

<embed>*(.*?)</embed> 

結果:

<embed src="http://www.youtube.com/v/Ora35AzLxt0?fs=1&amp;amp;hl=fr_FR" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed> 

是有可能只獲得src屬性的值?

謝謝!

回答

2

,請不要使用正則表達式的地方是不必要的...

var htmlcode = '<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/Ora35AzLxt0?fs=1&amp;hl=fr_FR"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/Ora35AzLxt0?fs=1&amp;hl=fr_FR" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>'; 

div = document.createElement('div'); 
div.innerHTML = htmlcode ; 
var yourSrc = div.getElementsByTagName('embed')[0].src; 
+0

yeahhh,非常感謝! – Christian 2010-09-30 15:26:53

2

除了已經提到的DOM方法,您還可以使用jQuery來爲你做這個,如果一直包含它(不由OP提到):

var htmlcode = '<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/Ora35AzLxt0?fs=1&amp;hl=fr_FR"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/Ora35AzLxt0?fs=1&amp;hl=fr_FR" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>'; 
var yourSrc = jQuery(htmlcode).find('embed').prop('src');