How to wrap text in a hyperlink ONLY if it is not already wrapped in a hyperlink

I need to do a regex replacement where I take a string and wrap a hyperlink around it (but here's the catch) if it is not already surrounded by a hyperlink. How can I do it?

So, for example, take the text:

The quick brown fox.

I want to make a "quick brown" link, for example:

The <a href="http://www.stackoverflow.com/"> quick brown </a> fox.

But if I find the text:

The <a href="http://www.stackoverflow.com/"> quick brown </a> fox.

I want to make sure that I do not wrap the “fast brown” in another hyperlink.

How can I do it?

+1
source share
4 answers

-. , , , .

(?<=>)quick brown(?=</a>)

. lookbehind ( , PCRE).

+1

, , YOUR_STRING, , YOUR_STRING .

regex = <a[^>]*>[^<]*(YOUR_STRING)[^<]*</a>

<a

0 , >.

>

0 , <.

YOUR_STRING .

0 , <.

</a>

, YOUR_STRING .

, YOUR_STRING , .

. : , , ConcurrentModificationException/ ( ). - StringBuffer , .

: ( html), html. . href, <a>quick brown fox</a>. html , , .

, .

+1

, , URL- , URL- "raw" html

+1

, html, , html?

0

Source: https://habr.com/ru/post/1715763/