如何仅从文本中删除<a>标签?

Mah*_*leh 3 java regex

问候所有,我有一个文本,可能包含以下<a></a>标签:

hello this is a link <a href="www.google.com"> www.google.com </a> please visit it.
Run Code Online (Sandbox Code Playgroud)

我想删除这些标记并将它们保持在它们之间:

hello this is a link  www.google.com  please visit it.
Run Code Online (Sandbox Code Playgroud)

, 怎么做 ?

zel*_*lio 9

仅适用于<a></a>标签

String source = "<a>blargle</a>";
source.replaceAll( "</?a>", "" );
Run Code Online (Sandbox Code Playgroud)

如果你的意思是<a>带有其他属性的标签,那么你需要

String source = "<a>blargle</a>";
source.replaceAll( "</?a[^>]*>", "" );
Run Code Online (Sandbox Code Playgroud)


Jig*_*shi 6

String str="<a>sadasd</a>";
str.replaceAll("<a>","").replaceAll("</a>","");//sadasd
Run Code Online (Sandbox Code Playgroud)

要么

 str.replaceAll("</?a>","");//sadasd
Run Code Online (Sandbox Code Playgroud)

或者最好的方法是选择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
Run Code Online (Sandbox Code Playgroud)