我有一個形象標籤下面,如何找到一個圖像標記的SRC與ALT在JS
var img = '<img src="http://fmp-8.cit.nih.gov/hembase/search2images/chrX.jpg" alt="image x">';
我想找到這個IMG的src,我寫了下面的代碼,
var rex = /<img[^>]+src="?([^"\s]+)"?[^>]*\/>/g;
while (m = rex.exec(text)) {
imageUrls.push(m[1]);
}
但我imageUrls在空甚至src元素在那裏。可以任何人請幫助我。謝謝。
var m, imageUrls = [],
rex = /<img[^>]+src="?([^"\s]+)"?[^>]*\/>/g;
while (m = rex.exec(text)) {
imageUrls.push(m[1]);
}console.log(imageUrls+'lslsls')
for (var i = 0; i < imageUrls.length; i++) {
text = text.replace(/<img[^>]*>/, '<amp-img layout="responsive" class="ampimageheight" src="' + imageUrls[i] + '" width="200" height= "100"></amp-img>');
}
這是爲[TO͇̹̺Ɲ̴ȳ̳寶NY](另一種情況下https://stackoverflow.com/questions/1732348/regex-match-open-標籤,除了xhtml自我包含標籤) – adeneo