简体   繁体   English

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

[英]How to remove certain html tags from a String with replaceAll?

I have a string including different kinds of html tags. 我有一个包含不同种类的html标签的字符串。

I want to remove all <a> and </a> tags. 我想删除所有<a></a>标记。

I tried: 我试过了:

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

But it doesn't work. 但这是行不通的。 Those tags still remain in the string. 这些标签仍保留在字符串中。 Why? 为什么?

Those tags still remain in the string. 这些标签仍保留在字符串中。 Why? 为什么?

Because replaceAll doesn't modify the string directly (it can't, strings are immutable), it returns the modified string. 因为replaceAll不能直接修改字符串(不能,字符串是不可变的),所以它返回修改后的字符串。 So: 所以:

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

Live Example 现场例子

Or 要么

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

Note that replaceAll takes a string defining a regular expression as its first argument. 请注意, replaceAll将定义正则表达式的字符串作为其第一个参数。 "<a>" and "</a>" are both fine, but unless you need to use a regular expression, use replace(CharSequence,CharSequence) instead. "<a>""</a>"都可以,但是除非需要使用正则表达式,否则请使用replace(CharSequence,CharSequence) If using replaceAll , just be aware of the characters with special meaning in regular expressions. 如果使用replaceAll ,请注意正则表达式中具有特殊含义的字符。

In fact, you can do it with one replaceAll by making use of the fact you're using regular expressions: 事实上,你可以用一个做到这一点replaceAll通过利用你使用正则表达式的事实:

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

The ? ? after the / makes the / optional, so that'll replace "<a>" and "</a>" . 之后, /使/可选,这样会取代"<a>""</a>"

Live Example 现场例子

replaceAll("\\<\\w*\\>", "\\ ").replaceAll("\\", "\\ "); replaceAll(“ \\ <\\ w * \\>”,“ \\”).replaceAll(“ \\”,“ \\”); remove all tags html XD , 2 "\\" 删除所有标签html XD,2“ \\”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM