HTML頁面中我有以下字符串:字符串替換關鍵字是使用Java
<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>
我想直到結束是:
<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>
所以幾乎我要搜索並用<b>foo</b>
或<b>Foo</b>
代替foo
。保留正在替換的字符串的情況很重要,但要將其與關鍵字foo
相匹配,同時忽略匹配的情況。
另一個重要的事情是替換忽略所有的html標籤及其內容。請注意,<div id="foo">
保持原樣。
我起草了這一點,但沒有測試尚未
text = text.replace("(?i)"+keyword+"(?!([^<]+)?>)", "<b>"+keyword+"</b>");
與上面的問題是,它不記得被替換單詞的情況下,只是把在關鍵字。
你剛剛回答我的回答是因爲我先回答了嗎? – adrianboimvaser 2011-01-22 00:13:39