Extract image src from a string

前端 未结 5 518
遇见更好的自我
遇见更好的自我 2020-11-30 06:02

I\'m trying to match all the images elements as strings,

This is my regex:

html.match(/]+src=\"http([^\">]+)/g);

Th

5条回答
  •  遥遥无期
    2020-11-30 06:29

    As Mathletics mentioned in a comment, there are other more straightforward ways to retrieve the src attribute from your tags such as retrieving a reference to the DOM node via id, name, class, etc. and then just using your reference to extract the information you need. If you need to do this for all of your elements, you can do something like this:

    var imageTags = document.getElementsByTagName("img"); // Returns array of  DOM nodes
    var sources = [];
    for (var i in imageTags) {
       var src = imageTags[i].src;
       sources.push(src);
    }
    

    However, if you have some restriction forcing you to use regex, then the other answers provided will work just fine.

提交回复
热议问题