I want to write a regular expression that will replace the word Paris by a link, for only the word is not ready a part of a link.
Example:
i'm living <a href="Paris" atl="Paris link">in Paris</a>, near Paris <a href="gare">Gare du Nord</a>, i love Paris.
would become
i'm living.........near <a href="">Paris</a>..........i love <a href="">Paris</a>.
This is hard to do in one step. Writing a single regex that does that is virtually impossible.
Try a two-step approach.
<a href="..."><a href="...">Paris</a></a>), and eliminate the inner link.Regex for step one is dead-simple:
\bParis\b
Regex for step two is slightly more complex:
(<a[^>]+>.*?(?!:</a>))<a[^>]+>(Paris)</a>
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.
Explanation of regex #2 in plain words:
<a[^>]+>), optionally followed by anything that is not itself followed by a closing link (.*?(?!:</a>)). Save it into match group 1.<a[^>]+>). Make sure it is there, but do not save it.</a>). Make sure it is there, but don't save it.The approach assumes these side conditions:
(?!:...)).Paris" becomes "<a href"...">Paris</a>", or step two will fail (until you change the second regex).BTW: regex #2 explicitly allows for constructs like this:
<a href="">in the <b>capital of France</b>, <a href="">Paris</a></a>
The surplus link comes from step one, replacement result of step 2 will be:
<a href="">in the <b>capital of France</b>, Paris</a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With