I would like to write a regular expression in javascript to match specific text, only when it is not part of an html link, i.e.
match <a href="/link/page1">match text</a>
would not be matched, but
match text
or
<p>match text</p>
would be matched.
(The "match text" will change each time the search is run - I will use something like
var tmpStr = new RegExp("\bmatch text\b","g");
where the value of "match text" is read from a database.)
So far my best effort at a regular expression is
\bmatch text\b(?!</a>)
This deals with the closing , but not the initial . This will probably work fine for my purposes, but it does not seem ideal. I'd appreciate any help with refining the regular expression.
You can use a negative look-behind to get the opening <a href=...:
var tmpStr = new RegExp('(?<!<a.*?>)match text(?!</a>)');
Hope that works for you.
Thanks for the very quick and helpful answers. Just to clarify, the regular expression I ended up using was
(?!<a.*?>)\bmatch text\b(?!</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