How to remove the <a> tags only from a text?
Greetings all,
I have a text that may contains the <a></a>
tags something like :
hello this is a link <a href="www.google.com"> www.google.com </a> p开发者_如何转开发lease visit it.
I want to remove those tags and keep the text between them to be like:
hello this is a link www.google.com please visit it.
, how to do ?
For just the <a>
and </a>
tags
String source = "<a>blargle</a>";
source.replaceAll( "</?a>", "" );
If you mean <a>
tags with other attributes then you would need
String source = "<a>blargle</a>";
source.replaceAll( "</?a[^>]*>", "" );
String str="<a>sadasd</a>";
str.replaceAll("<a>","").replaceAll("</a>","");//sadasd
Or
str.replaceAll("</?a>","");//sadasd
Or the best way is to go for Jsoup Cleaner
String str = "hello this is a link <a href='www.google.com'> www.google.com </a> please visit it";
String safe = Jsoup.clean(str, Whitelist.simpleText());
System.out.println(safe);//hello this is a link www.google.com please visit it
System.out.println(s.replaceAll("</?a>", ""));
Here you go: str.replaceAll("</?a>","")
if (message.contains("<a href=")) {
message = message.replaceAll("(.*)?<a.*?>", "").replaceAll("</a>", "");
}
Generally, it is a bad idea to fix HTML with a regular expression - for a discussion see the top answer here Using regular expressions to parse HTML: why not? - even though it looks like it might just be quick . Try instead to use a DOM parser in your language of choice and just replace the node with its child text node.
精彩评论