'Getting a image url from html srcset using JavaScript regex
I am trying to get an image url from an html that has an img srcset using javascript. I am particularly interested in getting 2nd or 3rd image.
Example of srcset within html:
<img srcset=\"http:example.jpg 140w,http:example.jpg 160w,http:example.jpg 320w,http:example.jpg 480w,http:example.jpg 720w,http:example.jpg 1280w,http:example.jpg 1500w\" src=\"http:example.jpg\" alt=\"example\">
I can only use pure javascript.
Any help will be much appreciated as I've already spent hours trying to get this work and the only success I had so far is to get all of those links out of html.
Solution 1:[1]
Using http:([^ ,]+) will match "example.jpg" in several spots. You can iterate through your results to get the one you're looking for.
Explanation
http:finds the literal[^ ,]+finds multiple non-space, non-comma characters.- Parenthesis creates a match.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
