0

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

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

TylerH
  • 20,799
  • 66
  • 75
  • 101
Sean Carroll
  • 107
  • 1
  • 14
  • any preferences for programming languages? – RomanPerekhrest Mar 20 '17 at 09:39
  • All pages are .php pages & I usually use regular expression in dreamweaver CC's Find & Replace box. – Sean Carroll Mar 20 '17 at 09:50
  • If your thinking a Script solution rather than a Physical Data Replace solution? if that is possible would be a great solution also. As for language? not really fazed PHP or Javascript as long as it will work on websites & mobile browsers. – Sean Carroll Mar 20 '17 at 09:58
  • Please add a program / language tag to your question which is going to be used for your regex. – revo Mar 20 '17 at 11:59
  • Hi Revo, I am using Dreamweaver search and replace, with regular expression enabled, not sure what language that is... I just tried using code from your answer below, but without success so I am assuming Dreamweavers Find / Replace regular expression isn't QregularExpression? I also clicked the link you provided & the pcre (PHP) example does exactly what I need however I have no idea how to apply to my 1000+ php pages, other than using Dreamweaver. Any suggestions.? – Sean Carroll Mar 20 '17 at 12:11
  • Also should be noted, whist the pages are php pages the data within is static HTML and not generated dynamically. not sure if this explains question any better. – Sean Carroll Mar 20 '17 at 12:26
  • I doubt if Dreamweaver supports PCRE thoroughly hence regex doesn't work in it. I suggest to load html data into another editor like Sublime text for this specific task. – revo Mar 20 '17 at 12:37
  • I downloaded Sublime text and loaded pages and had the same issue as I did with Dreamweavers find and replace. "Its finds and replaces data all over document not just in specific span. is there other settings I need to enable in Subime other than RegularExpression? Another note is that there is multiple spans, data & scripts above & below the but there are no other spans called TagsStyle. – Sean Carroll Mar 20 '17 at 13:16

1 Answers1

1

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

Community
  • 1
  • 1
revo
  • 47,783
  • 14
  • 74
  • 117