Suppose we have a string <span id='make-this-bold'>A giant pan is still a pan.</span>
I want to match the string pan
inside the content of the tag but not in the tag. In this example, the desired outcome would pan
and pan
from 'A giant pan is still a pan'. My initial thought was to use negative look ahead or behind, but I'll have to write different regex if I want to match other strings, like is
or old
.
To summarize, I'm trying to write a regex that looks for a string, like pan
, outside a specific pattern, <span id='make-this-bold'>
. How can I easily accomplish this?
Edit on Nov. 19th 2019
Thanks for reading and answering this. To clarify, the object we are dealing with is a string, not HTML elements as there are no HTML DOM elements that contain this text or any make-this-bold
tag at this point. The original goal is to inject one specific tag around certain words in a plain text so they can be rendered bold on the web page. In the current script it is treated as a string operation, which uses a for loop to go through a list of words that needs to be bold, search for each word in the string, and if found, inject the tag around it.
Therefore, as the question is framed, it is not to parse HTML but to parse a string, and the answer is not in this question - RegEx match open tags except XHTML self-contained tagsRegEx match open tags except XHTML self-contained tags.
Update
As a workaround, I'm simply replacing <span id='make-this-bold'>
and </span>
with a string constant before injection and revert them after injection. Still hope there's an elegant regex solution to do look for pattern A but ignore it if pattern A is in pattern B.