如何让正则表达式正确匹配带有正确图像文件名的 url? [英] How do I get Regex to correctly match urls with correct image file names?

查看:67
本文介绍了如何让正则表达式正确匹配带有正确图像文件名的 url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用正则表达式更新大型文档的路径,并且我正在尝试获得匹配以匹配包含文件类型 JPG、PNG 的所有 img Src 标签

I am trying to update paths of a large document using Regex and I am trying to get the match to match all img Src tags that include the file type JPG, PNG

我使用以下方法尝试让 Regex 正确匹配:

I have useded the following to try get Regex to correctly match:

<img src="xanne.nnn.pagespeed.ic.u49smximgo.jpg" alt="test">
<img src="xanne.nnn.pagespeed.ic.u49smximgo.png" alt="test">

但它也匹配底部

<img src="xanne.nnn.pagespeed.ic.u49smximgo.webp" alt="test">
<img src="xanne.nnn.pagespeed.ic.u49smximgo.gif" alt="test">

当我使用以下正则表达式时:

When I use the following Regex:

<img src="(?=.*(jpg|png)?)

有什么想法可以让它只匹配(1 和 2)而不是 1、2、3、4?

Any ideas how I can get it to match only (1 and 2) and not 1,2,3,4?

推荐答案

如果只想匹配 src 属性值

If you want to match only the src attribute value

img src="(?=(.*jpg|.*png))

RegEx 演示

这将匹配具有 1,2 所示格式的整行

This will match whole line that has of the format shown in 1,2

(?=^<img src=".*(jpg|png)").*$

这篇关于如何让正则表达式正确匹配带有正确图像文件名的 url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆