Regex用两个通配符查找,替换其中一个-TextMate



我需要在几十个不同的html文档中找到并替换数千个目标url(ahrefs)。。。所有ahref的格式都不同。我需要用一个统一的目标url替换所有不同的目标url。

有两个障碍:

  1. 我不想搞砸css文件引用等的linkhrefs,而是只更改实际的ahref

2.在这些文件中,ahrefs没有一个统一的结构。有些在a和href之间有class=的东西。示例包括:

 <a class='image' href="examplelinkone.com">
<a class='image ' href="examplelinkone.com"> ( space between e and ' )
<a class='someotherclass' href="examplelinktwo.com"

当我使用这样的regex时,我可以成功地查找ahrefs的所有实例。。。

<a[^<>]+href="[^<>]+"

但是我不知道如何只替换href=部分的双引号之间的内容,而忽略a和href

之间的内容

您需要使用捕获组。

(<a[^<>]+href=")[^<>]+(")

在替换零件中,你需要这样给出,

$1replacement-string$2

$1表示我们正在反向引用组索引1(<a[^<>]+href=")中存在的字符。接下来是双引号中的部分。此部分已被您作为替换字符串提供的字符串替换。最后,第二个捕获的组被反向引用以获得最后的"符号。

类似于此模式的东西应该可以消除href=:引号之间的任何内容

b(href=W)[ws.]+(?=W)b

替换为:

$1

--在TextMate:中测试

 <a class="image" href="examplelinkone.com">anything<a href="more">
<a class='image ' href='examplelinkone.com'> ( space between e and ' )"<something>"All ok"</a>
<a class='someotherclass' href="examplelinktwo.com"

结果:

 <a class="image" href="">anything<a href="">
<a class='image ' href=''> ( space between e and ' )"<something>"All ok"</a>
<a class='someotherclass' href=""

最新更新