Mahmoud Saleh
Mahmoud Saleh

Reputation: 33625

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> please 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 ?

Upvotes: 3

Views: 6391

Answers (6)

Alireza Ghanbarinia
Alireza Ghanbarinia

Reputation: 944

if (message.contains("<a href=")) {
    message = message.replaceAll("(.*)?<a.*?>", "").replaceAll("</a>", "");
}

Upvotes: 1

eHussain
eHussain

Reputation: 3377

Here you go: str.replaceAll("</?a>","")

Upvotes: 1

Spaceghost
Spaceghost

Reputation: 6995

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.

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240948

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

Upvotes: 6

user467871
user467871

Reputation:

System.out.println(s.replaceAll("</?a>", ""));

Upvotes: 1

zellio
zellio

Reputation: 32514

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[^>]*>", "" );

Upvotes: 9

Related Questions