I want to write a function that highlights some text. It takes a HTML string as input and returns HTML string with additional html tags.
Example: Input string (need to highlight the word "text"):
<div>
<a href="..." title="text to highlight">Some text to highlight</a>
<a href="..." title="text to highlight">Some other text to highlight</a>
</div>
Output string:
<div>
<a href="..." title="text to highlight">Some <b class="highlight">text</b> to highlight</a>
<a href="..." title="text to highlight">Some other <b class="highlight">text</b> to highlight</a>
</div>
I have found a regexp that matches text only between html tags, but I can't figure out how to surround some part of it with additional tags
highlight_str = u'text'
p = re.compile(r"[^<>]+开发者_运维问答(?=[<])")
iterator = p.finditer(search_str)
for match in iterator:
# code for replacement here ???
Is there any other ideas to do it?
Look at Beautiful Soup.
精彩评论