Question
How can you match an unknown pattern precisely enough to wrap it in some HTML highlighting tags?
Answer
Reverse-engineer the indexing process.
You have to parse the HTML document, on the fly, just like the search indexer did originally, while preserving all HTML markup as-is and not introducing non-standard HTML markup.