Regular expression to match specific unrelated text

I would like to write a regular expression in javascript to match a specific text only if it is not part of the html link, i.e.

match <a href="/link/page1">match text</a>

won't match but

match text

or

<p>match text</p>

will match.

(the "match text" will change every time a search is performed - I will use something like

var tmpStr = new RegExp("\bmatch text\b","g");

where the value "match text" is read from the database.)

So far my best effort with regular expression

\bmatch text\b(?!</a>)

This applies to closure, but not the initial one. This will probably work well for my purposes, but it does not seem ideal. I would appreciate help in clarifying the regular expression.

+3
2

, <a href=...:

var tmpStr = new RegExp('(?<!<a.*?>)match text(?!</a>)');

, .

+4

. , , ,

(?!<a.*?>)\bmatch text\b(?!</a>)
+3

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


All Articles