How can I extract src
attribute of embed tag with regex?
In this example(youtube video):
<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;hl=fr_FR"></param>
<param name="allowFullScreen" value="true"></param>
<param na开发者_如何学Cme="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>
</p>
</div>
</div>
I'm only able to extract the complete tag with this regex:
<embed>*(.*?)</embed>
result:
<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>
Is it possible to get only the value of src
attribute?
Thanks!
Please do not use regexp where it is unnecessary...
var htmlcode = '<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/Ora35AzLxt0?fs=1&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&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;
In addition to the DOM methods already mentioned, you can also use jQuery to do this for you if it's been included (wasn't mentioned by OP):
var htmlcode = '<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/Ora35AzLxt0?fs=1&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&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');
精彩评论