使用JavaScript正则表达式从html srcset获取图像url

Getting a image url from html srcset using JavaScript regex

本文关键字:获取 图像 url srcset html JavaScript 正则表达式 使用      更新时间:2023-12-28

我正试图使用javascript从一个具有img srcset的html中获取图像url。我对获得第二张或第三张照片特别感兴趣。

html:中的srcset示例

<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'">

我只能使用纯javascript。

任何帮助都将不胜感激,因为我已经花了几个小时试图完成这项工作,到目前为止,我唯一的成功是从html中获得了所有这些链接。

使用http:([^ ,]+)将匹配"example.jpg";在几个地方。你可以迭代你的结果,得到你想要的结果。

解释

  • http:查找文字
  • [^ ,]+查找多个非空格、非逗号字符
  • 圆括号创建匹配