使用正则表达式替换具有特定内容的锚链接

时间:2014-09-24 07:10:13

标签: regex

我在页面中有很多锚链接,并且想要将所有链接替换为"#",这是为了避免用户在演示期间跳转到另一个站点。

例如:

<ul>
    <li><a href="http://example.com/home/">Home</a></li>
    <li><a href="#">Demo</a>
        <ul>
            <li><a class="ahref" cid="1" href="http://www.example.com/1/">Demo 1</a>Demo 1</a></li>
            <li><a class="ahref" cid="2" href="http://www.example.com/2/">Demo 1</a>Demo 2</a></li>
            <li><a class="ahref" cid="3" href="http://www.example.com/3/">Demo 1</a>Demo 3</a></li>
            <li><a class="ahref" cid="4" href="http://www.example.com/4/">Demo 1</a>Demo 4</a></li>
        </ul>
    </li>
</ul>

如何搜索所有<a href="....">并将其替换为<a href="#">,但不包括所有其他属性。

1 个答案:

答案 0 :(得分:0)

搜索:(a.*?href\s*=\s*)['"].*?['"]

替换为:\1"#"

相关问题