简体   繁体   English

如何在 html 字符串中提取 class div 中的图像源

[英]How to extract the source of images inside class div in html string

I have regular expression for extracting the source of images in html string, that works fine.我有用于在 html 字符串中提取图像源的正则表达式,效果很好。 The output is in urls array: output 在 urls 数组中:

var m,
urls = [],
str = '<img class="d-block mx-auto" src="http://www.website.com/wp-content/uploads/2020/05/66189-759x493.jpg" />,
rex = /<img[^>]+src="?([^"\s]+)"?\s*\alt="First slide">/g;
      while ( m = rex.exec( str ) ) {
          urls.push( m[1] );
      }
console.log(urls);  // ["http://www.website.com/wp-content/uploads/2020/05/66189-759x493.jpg",...]

However I want rex that gets only the source of images inside class carousel-item:但是,我希望 rex 仅获取 class 轮播项目中的图像来源:

str = '<div class="carousel-item"> <img class="d-block mx-auto" src="http://www.website.com/wp-content/uploads/2020/05/66189-759x493.jpg" />';

I would skip the regular expression and use DOM methods to get to the content you want.我会跳过正则表达式并使用 DOM 方法来获取您想要的内容。

Using template or DomParser and querySelector使用模板或 DomParser 和 querySelector

 var str = '<div class="carousel-item"> <img class="d-block mx-auto" src="http://www.website.com/wp-content/uploads/2020/05/66189-759x493.jpg" /></div>'; var temp = document.createElement("template"); temp.innerHTML = str console.log(Array.from(temp.content.querySelectorAll(".carousel-item img")).map(x => x.src)) var doc = new DOMParser().parseFromString(str, "text/html") console.log(Array.from(doc.querySelectorAll(".carousel-item img")).map(x => x.src))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM