使用Java替换HTML页面字符串中的关键字

使用Java替换HTML页面字符串中的关键字,java,html,string,replace,Java,Html,String,Replace,我有以下字符串: <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>
<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>
上面的问题是,它不记得要替换的单词的大小写,只输入关键字。

您需要使用a,顺便说一句,使用
replaceAll

text = text.replaceAll("(?i)("+keyword+")(?!([^<]+)?>)", "<b>$0</b>");
text=text.replaceAll(“(?i)(“+关键字+”)(!([^)”,“$0”);
您需要使用,顺便说一句,使用
replaceAll

text = text.replaceAll("(?i)("+keyword+")(?!([^<]+)?>)", "<b>$0</b>");
text=text.replaceAll(“(?i)(“+关键字+”)(!([^)”,“$0”);
text.replaceAll(“(?i)(“+关键字+”)(!([^)”,“$1”)
text.replaceAll(“(?i)(“+关键字+”)(!([^)”,“$1”)

应如下所示,即应设置单词边界:

text.replaceAll("(?i)(\\b" + keyword + "\\b)(?!([^<]+)?>)", "<b>$1</b>")
text.replaceAll(“(?i)(\\b”+关键字+”\\b)(?!([^)”,“$1”)

应如下所示,即应设置单词边界:

text.replaceAll("(?i)(\\b" + keyword + "\\b)(?!([^<]+)?>)", "<b>$1</b>")
text.replaceAll(“(?i)(\\b”+关键字+”\\b)(?!([^)”,“$1”)

你是否仅仅因为我先回答就否决了我的答案?对整个比赛的反向引用显示为$0。如果有捕获括号,你可以引用具体的组为$1、$2等@adrianboimvaser:不!我没有!当我看到你的答案时,我实际上正要删除我的答案,但后来我意识到你写了$0…你这么做了吗我的答案是什么?:)@Costi Ciudatu:对不起,我看到我的第一张选票消失了。当然不是!@adrianboimvaser:我知道,我只是在开玩笑。也许我的“答案”应该只是对你的评论…@Mat Banik:你应该接受第一个答案,但我发布的小修正是:$1而不是$0:)你是不是因为我先回答就否决了我的答案?对整个比赛的反向引用显示为$0。如果有捕获括号,你可以引用具体的组为$1、$2等等@adrianboimvaser:不!我没有!当我看到你的答案时,我实际上正要删除我的答案,但后来我意识到你写了$0…你是用m写的吗y答案?:)@Costi Ciudatu:对不起,我看到我的第一张选票消失了。当然不是!@adrianboimvaser:我知道,我只是在开玩笑。也许我的“答案”应该只是对你的评论…@Mat Banik:你应该接受第一个答案,但我发布了一个小补丁:$1而不是$0:)replaceAll()接受正则表达式,而replace()只接受文本序列replaceAll()接受正则表达式,而replace()只接受文本序列
text.replaceAll("(?i)(\\b" + keyword + "\\b)(?!([^<]+)?>)", "<b>$1</b>")