使用DW中的正则表达式将属性从一个标签复制到另一个标签

时间:2015-09-21 16:24:12

标签: regex dreamweaver

我试图通过Dreamweaver中的正则表达式将标记从HTML中的一个位置复制到另一个位置。具体来说,我想采取以下代码:

<i class="icon-camera"></i></td>
<td class="lastName"><a name="smith" class="lbp_secondary" href="http://www.mywebsite.com/contact-us/directory/joe-smith/" rel="nofollow">Smith</a>

并启用正则表达式查找/替换,以便使用以下语法替换代码:

<a name="smith" class="lbp_secondary" href="http://www.mywebsite.com/contact-us/directory/joe-smith/" rel="nofollow"><i class="icon-camera"></i></a></td>
<td class="lastName"><a name="smith" class="lbp_secondary" href="http://www.mywebsite.com/contact-us/directory/joe-smith/" rel="nofollow">Smith</a>

基本上,它使用下一行中使用的相同标签包装标签对。

到目前为止我尝试过的查找/替换是:

查找

<i class="icon-camera"></i></td>
<td class="lastName"><a(.*)>(.*)</a>

替换:

<a$1><i class="icon-camera"></i></a></td>
<td class="lastName"><a$1>$2</a>

另外,为了清楚起见,我试图在我的HTML中存在大约300个(大约450个)实例中执行此操作。因此,要使用的一些示例数据如下所示:

<tr>
<td class="photo" style="text-align: center;" align="center"><i class="icon-camera"></i></td>
<td class="lastName"><a name="davis" class="lbp_secondary" href="http://www.mywebsite.com/contact-us/directory/kathy-aldrich/" rel="nofollow">Davis</a></td>
<td class="firstName"><a name="david" class="lbp_secondary" href="http://www.mywebsite.com/contact-us/directory/kathy-aldrich/" rel="nofollow">David</a></td>
<td class="businessPhone">509-555-2353</td>
<td class="emailAddress"><a href="mailto:davidd@mywebsite.com">davidd@mywebsite.com</a></td>
<td class="office">1822</td>
<td class="department">Shipping</td>
</tr>
<tr>
  <td class="photo" style="text-align: center;" align="center"><i class="icon-camera"></i></td>
  <td class="lastName"><a name="allen" class="lbp_secondary" href="http://www.mywebsite.com/contact-us/directory/alan-allen/" rel="nofollow">Allen</a></td>
  <td class="firstName"><a name="alan" class="lbp_secondary" href="http://www.mywebsite.com/contact-us/directory/alan-allen/" rel="nofollow">Alan</a></td>
  <td class="businessPhone">509-555-2027</td>
  <td class="emailAddress"><a href="mailto:alana@mywebsite.com">alana@mywebsite.com</a></td>
  <td class="office">1481</td>
  <td class="department">Marketing</td>
</tr>
<tr>
  <td class="photo" style="text-align: center;" align="center">&nbsp;</td>
  <td class="lastName"><a name="buttons" class="lbp_secondary" href="http://www.mywebsite.com/contact-us/directory/benjamin-buttons/" rel="nofollow">Buttons</a></td>
  <td class="firstName"><a name="benjamin" class="lbp_secondary" href="http://www.mywebsite.com/contact-us/directory/benjamin-buttons/" rel="nofollow">Benjamin</a></td>
  <td class="businessPhone">509-555-2250</td>
  <td class="emailAddress"><a href="mailto:benjamina@mywebsite.com">benjaminb@mywebsite.com</a></td>
  <td class="office">3013</td>
  <td class="department">HR</td>
</tr>
<tr>
  <td class="photo" style="text-align: center;" align="center"><i class="icon-camera"></i></td>
  <td class="lastName"><a name="Lenore" class="lbp_secondary" href="http://www.mywebsite.com/contact-us/directory/luis-lenore/" rel="nofollow">Lenore</a></td>
  <td class="firstName"><a name="luis" class="lbp_secondary" href="http://www.mywebsite.com/contact-us/directory/luis-lenore/" rel="nofollow">Luis</a></td>
  <td class="businessPhone">509-555-2042</td>
  <td class="emailAddress"><a href="mailto:luisl@mywebsite.com">luisl@mywebsite.com</a></td>
  <td class="office">1432</td>
  <td class="department">Support</td>
</tr>

2 个答案:

答案 0 :(得分:0)

查找框中,输入以下内容:

(<i class="icon-camera"></i>)(</td>)

并在替换框中:

<a name="smith" class="lbp_secondary" href="http://www.mywebsite.com/contact-us/directory/joe-smith/" rel="nofollow">$1</a>$2

答案 1 :(得分:0)

要查找<i class="icon-camera"></i>的第一个出现并将其包含在下一个<a> tag的副本中:

查找

(<i class="icon-camera"></i>)([\s\S]*?)(<a [^>]*>)

替换:

$3$1</a>$2$3

DEMO

  • 请注意,我使用了[\s\S]*?,它与.*?完全相同,但它也会匹配换行符。
  • 额外的 ? 会生成量词lazy