2017-10-13 68 views
2

我正在纠正几个工作页面上的验证问题。由于这些页面是上次审查的,所以编码表格的正确方法是将<tfoot>置于</thead><tbody>之间。从那时起,决定验证的权力决定<tfoot>是要追溯到</tbody>。有些页面需要移动数十个表格。用RegEx移动Dreamweaver中的代码块

我想知道是否有一种方法可以</thead><tbody>之间捕捉一切与</tbody>后移动。我对正则表达式有一个基本的了解,但我无法弄清楚如何让它找到页脚内容。所有页脚的内容都不一样。

例子:

<tfoot> 
    <tr> 
     <td class="small text-left" colspan="6">Source: Mintel <abbr title="Global New Products Database">GNPD</abbr>, 2015.<br> 
     Note: rankings are based on 2014 data and <abbr title="Global New Products Database">GNPD</abbr> search was based solely 
     on products that contained a form of the word "flax."</td> 
    </tr> 
    </tfoot> 

而且

<tfoot> 
    <tr> 
     <td class="small text-left" colspan="5">Source: Global Trade Atlas, 
     2015 <br> 
     Compound Annual Growth Rate (CAGR) </td> 
    </tr> 
    </tfoot> 

基本上,把这样的事情:

<table class="table table-bordered text-right table-condensed mrgn-tp-lg"> 
<caption>Top 5 Pet Food Companies Worldwide in 2014, US$</caption> 
    <thead> 
<tr> 
    <th scope="col" class="active text-center">Company</th> 
    <th scope="col" class="active text-center">International Sales</th> 
    <th scope="col" class="active text-center">Sales in the EU</th> 
    </tr> 
</thead> 
<tfoot> 
<tr> 
<td class="small text-left" colspan="3">Source: Euromonitor International, 2015</td> 
</tr> 
</tfoot> 
<tbody> 
    <tr> 
    <td><b>1. Mars <abbr title="Incorporated">Inc.</abbr></b></td> 
    <td>$17.8 billion</td> 
    <td>$5.7 billion</td> 
    </tr> 
    <tr> 
    <td><b>2. Nestlé <abbr lang="fr" xml:lang="fr" title="Société Anonym">SA</abbr></b></td> 
    <td>$16.8 billion</td> 
    <td>$4.1 billion</td> 
    </tr> 
    <tr> 
    <td><b>3. Colgate-Palmolive <abbr title="Company">Co</abbr></b></td> 
    <td>$3.7 billion</td> 
    <td>$0.7 billion</td> 
    </tr> 
    <tr> 
    <td><b>4. Big Heart Pet Brand</b></td> 
    <td>$2.9 billion</td> 
    <td>Not available (N/A)</td> 
    </tr> 
    <tr> 
    <td><b>5. Blue Buffalo <abbr title="Company">Co</abbr> <abbr title="Limited">Ltd</abbr></b></td> 
    <td>$1.4 billion</td> 
    <td>N/A</td> 
    </tr> 
</tbody> 
</table> 

到这一点:

<table class="table table-bordered text-right table-condensed mrgn-tp-lg"> 
<caption>Top 5 Pet Food Companies Worldwide in 2014, US$</caption> 
    <thead> 
<tr> 
    <th scope="col" class="active text-center">Company</th> 
    <th scope="col" class="active text-center">International Sales</th> 
    <th scope="col" class="active text-center">Sales in the EU</th> 
    </tr> 
</thead> 

<tbody> 
    <tr> 
    <td><b>1. Mars <abbr title="Incorporated">Inc.</abbr></b></td> 
    <td>$17.8 billion</td> 
    <td>$5.7 billion</td> 
    </tr> 
    <tr> 
    <td><b>2. Nestlé <abbr lang="fr" xml:lang="fr" title="Société Anonym">SA</abbr></b></td> 
    <td>$16.8 billion</td> 
    <td>$4.1 billion</td> 
    </tr> 
    <tr> 
    <td><b>3. Colgate-Palmolive <abbr title="Company">Co</abbr></b></td> 
    <td>$3.7 billion</td> 
    <td>$0.7 billion</td> 
    </tr> 
    <tr> 
    <td><b>4. Big Heart Pet Brand</b></td> 
    <td>$2.9 billion</td> 
    <td>Not available (N/A)</td> 
    </tr> 
    <tr> 
    <td><b>5. Blue Buffalo <abbr title="Company">Co</abbr> <abbr title="Limited">Ltd</abbr></b></td> 
    <td>$1.4 billion</td> 
    <td>N/A</td> 
    </tr> 
</tbody> 
<tfoot> 
<tr> 
<td class="small text-left" colspan="3">Source: Euromonitor International, 2015</td> 
</tr> 
</tfoot> 
</table> 
+0

只有在假设条件下才有可能。 –

+0

这是什么意思? – Ferkner

+0

无法安全地使用正则表达式替换HTML内容。只有当你知道你想要匹配的值只发生在明确的上下文中,你可以安全地使用正则表达式。 –

回答

0

您可以使用

(</thead>\s*)([\s\S]*?)\s*(<tbody>[\s\S]*?</tbody>) 

regex demo。替换为$1$3\n$2

详细

  • (</thead>\s*) - 第1组:一个</thead>子串和0+空格(\s*
  • ([\s\S]*?) - 组2:任何0+字符,尽可能少,达到最左边的发生后续子模式
  • \s* - 0+空格
  • (<tbody>[\s\S]*?</tbody>) - 组3:
    • <tbody> - 一个<tbody>
    • [\s\S]*? - 任何0+字符,尽可能少的,最多的最左边的发生...
    • </tbody> - 一个</tbody>子。

$1$3\n$2替换匹配与第1组的值,则第3组值,然后插入一个换行,然后第2组值。