2017-10-13 64 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組值。