0

我有 1000 多个页面,我需要将某些单词转换为包含所述单词的链接。

基本上想知道我如何使用正则表达式来做类似...

改变。

<span class="TagsTStyle">PRODUCTS / SERVICES:</span>
<span class="TagsStyle">ACCOUNTANT, TAX, FINANCIAL PLANNING, GST, BAS, TAX RETURNS</span>

进入..

<span class="TagsTStyle">PRODUCTS / SERVICES:</span>
<span class="TagsStyle"><a href="../search.php?searchQuery=ACCOUNTANT">ACCOUNTANT</a>, <a href="../search.php?searchQuery=TAX">TAX</a>, <a href="../search.php?searchQuery=FINANCIA+PLANNING">FINANCIAL PLANNING</a>, <a href="../search.php?searchQuery=GST">GST</a>, <a href="../search.php?searchQuery=BAS">BAS</a>, <a href="../search.php?searchQuery=TAX+RETURNS">TAX RETURNS</a></span>

我有 1000 多页,每页上的文字都不同。

需要在所有页面上链接的关键字都在命名中span

<span class="TagsStyle">

并且每个单词,短语都用逗号分隔span

我很确定它可以使用正则表达式来实现,但这对于我来说有点太复杂了,无法绕开我的脑袋并制定一个表达式来做到这一点。

页面中使用的 html 示例如下:

<div align="center">
      <span class="CatTStyle">Category:</span>
      <span class="CatStyle">PHYSIOTHERAPY</span>
      <br>
      <br>
      <span class="BusTStyle">Business Name:</span>
      <span class="BusStyle">Physio</span>
      <br>
      <span class="PhTStyle">Phone:</span>
      <span class="PhStyle"><a onclick="_gaq.push(['_trackEvent', 'Phone', 'Click to Call', document.title])" href="tel:555 5555">555 5555 <img src="img/call.png"></a></span>
      <br>
      <span class="AddrTStyle">Address:</span>
      <span class="AddrStyle">1 Street Rd, Town, Country</span>
      <br>
      <span class="EmlTStyle">Email:</span>
      <span class="EmlStyle"><a onclick="_gaq.push(['_trackEvent', 'Email', 'Click to Email', document.title])" href="mailto:email@email.com">email@email.com</a></span>
      <br>
      <br>
      <span class="WsTStyle">Website:</span>
      <span class="WsStyle"><a onclick="_gaq.push(['_trackEvent', 'Website', 'Click to Website', document.title])" href="http://www.webiste.com">www.website.com</a></span>
      <br>
      <br>
      <span class="TagsTStyle">PRODUCTS / SERVICES:</span>
      <span class="TagsStyle">PHYSIOTHERAPY, BACK PAIN, SPINE INJURY</span>
      <br>
    </div>
<script async type="text/javascript">
    if ($(window).width() > 800) {document.write("</td><td align='center' valign='top' width='350'>");}
    if ($(window).width() < 800) {document.write("</td></tr><tr><td align='center' valign='top' width='350'>");} 
  </script> 
    <br>
    <div id="map" align="left" style="text-align:left;"></div>
    <script type="text/javascript">
      var address='1 Street Rd, Town, Country';
      var map = new google.maps.Map(document.getElementById('map'), {
        mapTypeId: google.maps.MapTypeId.TERRAIN,
        zoom: 15
      });
      var geocoder = new google.maps.Geocoder();
      geocoder.geocode({
        'address': address
      },
      function(results, status) {
        if(status == google.maps.GeocoderStatus.OK) {
          new google.maps.Marker({
          position: results[0].geometry.location,
          map: map
        });
        map.setCenter(results[0].geometry.location);
        }
      });
    </script>

但是请注意,我只希望为每个逗号分隔值<span class="TagsStyle"> 及其结束跨度创建链接</span>

4

1 回答 1

1

更新#1

由于QRegularExpression实现了与 Perl 兼容的正则表达式,您可以从 match resetter token\K\Gassertion中受益:

(<span\b[^"]+class="TagsStyle"[^>]*>|(?!\A)\G)([^,<]+)(,?\s*)

替换字符串:

\1<a href="../search.php?searchQuery=\2">\2</a>\3

现场演示

于 2017-03-20T09:37:46.377 回答