Search code examples
htmlregexdreamweaverqregularexpression

Complex Regular Expression - Change HTML words into links that search that word


I have 1000+ pages I need to turn certain words into links containing said word(s).

Basically wondering how I could use Regular Expression to do something like...

change.

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

into..

<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>

I have 1000+ pages and the words are different on each page.

Keywords that need to be linked on all pages are within named span

<span class="TagsStyle">

And each and all words, phrases are separated by commas within span.

I am pretty sure its possible to achieve using Regular expression, but this one is a little too complex for me to wrap my head around and work out an expression to do so.

an example of of html used in pages are:

<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 protected]">[email protected]</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>

However please note, I only wish to create links for each comma separated value(s) between <span class="TagsStyle"> and its closing span </span>


Solution

  • Update #1

    Since QRegularExpression implements Perl-compatible regular expressions you are able to benefit from match resetter token \K and \G assertion:

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

    Replacement string:

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

    Live demo