2010-09-29 4 views
1

Comment puis-je extraire l'attribut src de la balise embed avec regex?Comment puis-je extraire l'attribut src de la balise embed

Dans cet exemple (vidéo 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> 

Je suis seulement capable d'extraire l'étiquette avec ce regex:

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

Résultat:

<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> 

Est -il possible d'obtenir seulement la valeur de src attribut?

Merci!

Répondre

2

S'il vous plaît ne pas utiliser regexp où il est inutile ...

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, merci beaucoup! – Christian

2

En plus des méthodes DOM déjà mentionnés, vous pouvez également utiliser jQuery pour le faire pour vous si elle a été inclus (n'a pas été mentionné par 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'); 
Questions connexes