Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is hard to do in one step. Writing a single regex that does that is virtually impossible.</p> <p>Try a two-step approach.</p> <ol> <li>Put a link around every "Paris" there is, regardless if there already is another link present.</li> <li>Find all incorrectly nested links (<code>&lt;a href="..."&gt;&lt;a href="..."&gt;Paris&lt;/a&gt;&lt;/a&gt;</code>), and eliminate the inner link.</li> </ol> <p>Regex for step one is dead-simple:</p> <pre><code>\bParis\b </code></pre> <p>Regex for step two is slightly more complex:</p> <pre><code>(&lt;a[^&gt;]+&gt;.*?(?!:&lt;/a&gt;))&lt;a[^&gt;]+&gt;(Paris)&lt;/a&gt; </code></pre> <p>Use that one on the whole string and replace it with the content of match groups 1 and 2, effectively removing the surplus inner link.</p> <p>Explanation of regex #2 in plain words:</p> <ul> <li>Find every link (<code>&lt;a[^&gt;]+&gt;</code>), optionally followed by anything that is not itself followed by a closing link (<code>.*?(?!:&lt;/a&gt;)</code>). Save it into match group 1.</li> <li>Now look for the next link (<code>&lt;a[^&gt;]+&gt;</code>). Make sure it is there, but do not save it.</li> <li>Now look for the word Paris. Save it into match group 2.</li> <li>Look for a closing link (<code>&lt;/a&gt;</code>). Make sure it is there, but don't save it.</li> <li>Replace everything with the content of groups 1 and 2, thereby losing everything you did not save.</li> </ul> <p>The approach assumes these side conditions:</p> <ul> <li>Your input HTML is not horribly broken.</li> <li>Your regex flavor supports non-greedy quantifiers (.*?) and zero-width negative look-ahead assertions (<code>(?!:...)</code>).</li> <li>You wrap the word "Paris" only in a link in step 1, no additional characters. Every "<code>Paris</code>" becomes "<code>&lt;a href"..."&gt;Paris&lt;/a&gt;</code>", or step two will fail (until you change the second regex).</li> <li><p>BTW: regex #2 explicitly allows for constructs like this:</p> <p><code>&lt;a href=""&gt;in the &lt;b&gt;capital of France&lt;/b&gt;, &lt;a href=""&gt;Paris&lt;/a&gt;&lt;/a&gt;</code></p> <p>The surplus link comes from step one, replacement result of step 2 will be:</p> <p><code>&lt;a href=""&gt;in the &lt;b&gt;capital of France&lt;/b&gt;, Paris&lt;/a&gt;</code></p></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload