Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用replaceAll从字符串中删除某些html标记?_Java_Html_Parsing_Tags_Replaceall - Fatal编程技术网

Java 如何使用replaceAll从字符串中删除某些html标记?

Java 如何使用replaceAll从字符串中删除某些html标记?,java,html,parsing,tags,replaceall,Java,Html,Parsing,Tags,Replaceall,我有一个字符串,包括不同种类的html标记 我想删除所有和标记 我试过: string.replaceAll("<a>", ""); string.replaceAll("</a>", ""); string.replaceAll(“,”); string.replaceAll(“,”); 但它不起作用。这些标记仍然保留在字符串中。为什么? 这些标记仍然保留在字符串中。为什么? 因为replaceAll不直接修改字符串(不能,字符串是不可变的),所以它返回修改后的字符

我有一个字符串,包括不同种类的html标记

我想删除所有
标记

我试过:

string.replaceAll("<a>", "");
string.replaceAll("</a>", "");
string.replaceAll(“,”);
string.replaceAll(“,”);
但它不起作用。这些标记仍然保留在字符串中。为什么?

这些标记仍然保留在字符串中。为什么?

因为
replaceAll
不直接修改字符串(不能,字符串是不可变的),所以它返回修改后的字符串。因此:

string = string.replaceAll("<a>", "");
string = string.replaceAll("</a>", "")

/
后面的
使
/
成为可选的,因此将替换
“replaceAll(“\”,“\”)。replaceAll(“\”,“\”);删除所有标记html XD,2”\“

字符串是不可变的;每个修改字符串的操作都会返回一个字符串。不要在HTML/XML上使用正则表达式。还向我们展示示例inputBetter是解析html,而不是使用regex删除标记。。并不是地球上HTML文件中的所有操作都应该使用解析器。当您键入问题(以及之前的31个问题)时,右边有一个大橙色的“如何格式化”框。值得一读。还有一个完整的格式化工具条。还有一个[?]按钮,上面有很多有用的信息。以及文本框下的预览区域。当你写五个答案的时候,这些东西大部分都在那里。请花点时间学习如何使用它们。回答得很好,也很有帮助。或者replaceAll(“\\]+)\\>”,“\\”。replaceAll(“\\s+”,“)。trim()
string = string.replaceAll("<a>", "").replaceAll("</a>", "")
string = string.replaceAll("</?a>", "");