-3

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dmytro
  • 1
  • 1
  • 2

1 Answers1

-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.
Laurel
  • 5,965
  • 14
  • 31
  • 57