Java 在段落中查找非HTML标记词

Java 在段落中查找非HTML标记词,java,jsoup,Java,Jsoup,我有这样的想法: <p id="tire">I need new tires for my car</p> 我的车需要新轮胎 我试着写一些突出显示我分配的单词的东西,但不是被认为是标签的东西。例如,如果我想突出显示“轮胎”,理论上我会看到: <p id="tire">I need new <strong>tire</strong>s for my car</p> 我的车需要新的轮胎 但不幸的是,我看到: <p

我有这样的想法:

<p id="tire">I need new tires for my car</p>

我的车需要新轮胎

我试着写一些突出显示我分配的单词的东西,但不是被认为是标签的东西。例如,如果我想突出显示“轮胎”,理论上我会看到:

<p id="tire">I need new <strong>tire</strong>s for my car</p>

我的车需要新的轮胎

但不幸的是,我看到:

<p id="<strong>tire</strong>">I need new <strong>tire</strong>s for my car</p>

我的车需要新的轮胎


我只使用了一个简单的replaceAll(oldWord,newFormat)。有图书馆可以帮忙吗?我正在使用jsoup获取要搜索的HTML。

使用查找和替换,在单词前面添加一个空格,如“轮胎”


在replace
tires
中,使用find and replace,在单词前面添加一个空格,如“tires”


在replace
tires
中,使用find and replace,在单词前面添加一个空格,如“tires”


在replace
tires
中,使用find and replace,在单词前面添加一个空格,如“tires”

并在更换轮胎中尝试:

replaceAll("tire", "<strong>tire</strong>");
replaceAll("id=\"<strong>tire</strong>\"", "id=\"tire\"");
replaceAll(“轮胎”、“轮胎”);
replaceAll(“id=\”轮胎\”,“id=\“轮胎”);
这解决了特定的问题,但您可以找到其他问题,我想

试试:

replaceAll("tire", "<strong>tire</strong>");
replaceAll("id=\"<strong>tire</strong>\"", "id=\"tire\"");
replaceAll(“轮胎”、“轮胎”);
replaceAll(“id=\”轮胎\”,“id=\“轮胎”);
这解决了特定的问题,但您可以找到其他问题,我想

试试:

replaceAll("tire", "<strong>tire</strong>");
replaceAll("id=\"<strong>tire</strong>\"", "id=\"tire\"");
replaceAll(“轮胎”、“轮胎”);
replaceAll(“id=\”轮胎\”,“id=\“轮胎”);
这解决了特定的问题,但您可以找到其他问题,我想

试试:

replaceAll("tire", "<strong>tire</strong>");
replaceAll("id=\"<strong>tire</strong>\"", "id=\"tire\"");
replaceAll(“轮胎”、“轮胎”);
replaceAll(“id=\”轮胎\”,“id=\“轮胎”);

这解决了特定的问题,但是您可以获得其他的问题,我认为您可以使用选择方法
getElementsContainingOwnText(String searchText)
来选择包含您要查找的单词的元素。在这种情况下,“轮胎”

例如,它是如何工作的:

虚拟HTML

<html>
 <head></head>
 <body> 
  <p id="tire">I need new tires for my car</p>
 </body>
</html>

我的车需要新轮胎

我们的Jsoup代码:

Elements e = doc.getElementsContainingOwnText("tire");
for (Element el : e) {
    el.text(el.ownText().replace("tire", "<strong>tire</strong>"));
}
Elements e=doc.getElementsContainingOwnText(“轮胎”);
对于(元素el:e){
el.text(el.ownText().replace(“tire”,“tire”);
}
生成的文档打印输出:

<html>
 <head></head>
 <body> 
  <p id="tire">I need new <strong>tire</strong>s for my car</p>
 </body>
</html>

我的车需要新的轮胎


您可以使用选择方法
getElementsContainingOwnText(字符串搜索文本)
选择包含要查找的单词的元素。在这种情况下,“轮胎”

例如,它是如何工作的:

虚拟HTML

<html>
 <head></head>
 <body> 
  <p id="tire">I need new tires for my car</p>
 </body>
</html>

我的车需要新轮胎

我们的Jsoup代码:

Elements e = doc.getElementsContainingOwnText("tire");
for (Element el : e) {
    el.text(el.ownText().replace("tire", "<strong>tire</strong>"));
}
Elements e=doc.getElementsContainingOwnText(“轮胎”);
对于(元素el:e){
el.text(el.ownText().replace(“tire”,“tire”);
}
生成的文档打印输出:

<html>
 <head></head>
 <body> 
  <p id="tire">I need new <strong>tire</strong>s for my car</p>
 </body>
</html>

我的车需要新的轮胎


您可以使用选择方法
getElementsContainingOwnText(字符串搜索文本)
选择包含要查找的单词的元素。在这种情况下,“轮胎”

例如,它是如何工作的:

虚拟HTML

<html>
 <head></head>
 <body> 
  <p id="tire">I need new tires for my car</p>
 </body>
</html>

我的车需要新轮胎

我们的Jsoup代码:

Elements e = doc.getElementsContainingOwnText("tire");
for (Element el : e) {
    el.text(el.ownText().replace("tire", "<strong>tire</strong>"));
}
Elements e=doc.getElementsContainingOwnText(“轮胎”);
对于(元素el:e){
el.text(el.ownText().replace(“tire”,“tire”);
}
生成的文档打印输出:

<html>
 <head></head>
 <body> 
  <p id="tire">I need new <strong>tire</strong>s for my car</p>
 </body>
</html>

我的车需要新的轮胎


您可以使用选择方法
getElementsContainingOwnText(字符串搜索文本)
选择包含要查找的单词的元素。在这种情况下,“轮胎”

例如,它是如何工作的:

虚拟HTML

<html>
 <head></head>
 <body> 
  <p id="tire">I need new tires for my car</p>
 </body>
</html>

我的车需要新轮胎

我们的Jsoup代码:

Elements e = doc.getElementsContainingOwnText("tire");
for (Element el : e) {
    el.text(el.ownText().replace("tire", "<strong>tire</strong>"));
}
Elements e=doc.getElementsContainingOwnText(“轮胎”);
对于(元素el:e){
el.text(el.ownText().replace(“tire”,“tire”);
}
生成的文档打印输出:

<html>
 <head></head>
 <body> 
  <p id="tire">I need new <strong>tire</strong>s for my car</p>
 </body>
</html>

我的车需要新的轮胎


我不知道库的情况,但是你可以在字符串中循环,当你点击一个“”时设置一个标志,只要标志为false,你就不在标记中(假设有效的html,并且没有数据具有“值得一试的-看起来真的很慢,但是如果我有很多html,不是吗?人们总是将“循环”属性设置为“慢”。后面的算法”replaceAll“必须循环整个字符串。Java本机方法并不神奇:)我想我可能有点误解了这个问题。你是如何用正则表达式替换这个词的?我不知道库的情况,但你可以循环字符串,当你点击“”时设置一个标志,只要标志为假,你就不在标记中(假设是有效的html,并且没有数据包含“值得一试——看起来很慢,但是如果我有很多html,不是吗?人们总是将“循环”属性设置为“慢”。“replaceAll”后面的算法必须循环整个字符串。Java本机方法并不神奇:)我想我可能有点误解了这个问题。你是如何用正则表达式来替换这个词的?我不知道库的情况,但是你可以在你的字符串中循环,当你点击“”时设置一个标志,只要标志为false,你就不在标记中(假设有效的html,并且没有数据)“值得一试——如果我有很多HTML的话,看起来真的很慢,不是吗?人们总是用“慢”来定义“循环”。在“replaceAll”后面的算法必须循环整个字符串。Java本机方法不是