regex - 匹配所有具有特定 src 值的 img 标签
问题描述
我有一个这样的html:
<script>
$(“table:first”).after(“<a href=\“https://example.com/test123/\”
style=\“display: block; margin-left: auto; margin-right: auto; margin-
bottom: 116px;\“><img
src=\“https://example.com/images/banners/image.png\” alt=\“Example\”
width=\“88px\” style=\“display: block; margin-left: auto; margin-right: auto; \“></a>“);
</script>
<img src="https://example.com/images/banners/image1.png" alt="image">
https://example.com/images/banners/image1.png
我需要使用 url:和搜索所有 img 标签
https://example.com/images/banners/image.png
。只有当我匹配这些网址之一时,我才需要返回 true。我该怎么做?我的模式:
`<img.+?src=[\"']((?:https://example.com/images/banners/image1.png?|www).*)[\"'].*?>`
但我得到错误:
https://regexr.com/4capb
解决方案
您需要转义单线 (/) 字符,这在正则表达式中具有特殊含义。
所以:
<img.+?src=[\"']((?:https:\/\/example.com\/images\/banners\/image1.png?|www).*)[\"'].*?>
推荐阅读
- google-cloud-platform - 如何在谷歌云平台创建 MariaDB 数据库?
- r - R:根据特定规则重塑数据框
- java - 如何对 listView 中输入的数字进行加法?我试过这种方式它不起作用
- java - 向后移植应用程序时发生 ConnectivityManager NullPointerException
- java - 什么是正则表达式?
- mysql - 如何在 group by 语句后提取具有最大值的行?
- vue.js - 无法路由到我已经在 router.beforeEach 中的路由
- http - 总共有多少个 HTTP 状态码?
- email - Microsoft Graph API:使用 Graph Explorer 对“我的邮件”进行示例查询并收到错误 403
- xml - XSLT 检查 xml 元素是否存在,否则跳过父元素