I have following string:
<html>
<head><meta>...</meta><head>
<body>
<div id="foo">
Text I want to search & replace occurrences
of keywords such as Foo or foo while ignoring case
</div>
</body>
</html>
What I want to end up with is:
<html>
<head><meta>...</meta><head>
<body>
<div id="foo">
Text I want to search & replace occurrences
of keywords such as <b>Foo</b> or <b>foo</b> while ignoring case
</div>
</body>
</html>
So pretty much I’d like to search for and replace foo with <b>foo</b> or <b>Foo</b>. It is important to retain the case of the string that is being replace but match it with keyword foo while ignoring the case of the matches.
Another important thing is that the replacement ignores all html tags and their content. Notice that <div id="foo"> stays like it is.
I drafted this but didn’t test it yet
text = text.replace("(?i)"+keyword+"(?!([^<]+)?>)", "<b>"+keyword+"</b>");
The problem with the above is that it doesn’t remember the case of word that is being replaced and just puts in the keyword.
1 Answer