Pattern library/HTML img src / data-src regex

HTML img src / data-src regex

Handy for scrapers dealing with lazy-loaded carousels. Capture group holds the URL.

Expression

/(?<=(?:data-src|src)\s*=\s*['"])[^'"]+/gi

How to read it

  • (?<=(?:data-src|src)\s*=\s*['"])After src or data-src quote (lookbehind)
  • [^'"]+Full match is the image URL

Sample test

2 match(es)
<img src="https://cdn.example.com/a.jpg" />
<img data-src='https://cdn.example.com/lazy.webp' class="lazy" />

Starter prompt: extract img src and data-src from HTML