首页 > 解决方案 > 匹配所有具有特定 src 值的 img 标签

问题描述

我有一个这样的html:

<script>
    $(“table:first”).after(“&lt;a href=\“https://example.com/test123/\” 
    style=\“display: block; margin-left: auto; margin-right: auto; margin- 
    bottom: 116px;\“&gt;<img 
    src=\“https://example.com/images/banners/image.png\” alt=\“Example\” 
    width=\“88px\”  style=\“display: block; margin-left: auto; margin-right: auto; \“&gt;</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

标签: regex

解决方案


您需要转义单线 (/) 字符,这在正则表达式中具有特殊含义。

所以:

 <img.+?src=[\"']((?:https:\/\/example.com\/images\/banners\/image1.png?|www).*)[\"'].*?>

推荐阅读