Lizza
Lizza

Reputation: 2819

How can I remove all <a> tags from an HTML block, and leave the rest?

I have a bunch of html that I am prepping for display in a UIWebView for an iPad app I am working on. The HTML has a number of different tags that I am fine with, but there are a bunch of a-tags with irrelevant links that I need to have removed. I am going to be putting this text into a sqlite db.

What is the best way to get all of the a-tags out of my HTML text? I figure regex is the best way, but I just don't get regex very well. A blog online mentioned that this regex is the way to remove all tags:

 <(.|\n)*?>

So what would I need to do in order to adjust that to be a-tag specific? Or is there a different approach that I should take?

Thanks!

Upvotes: 1

Views: 139

Answers (2)

Efrain
Efrain

Reputation: 3364

the regex you need is:

<a.*?>|</a>

this matches both <a{something}> OR </a> - the tags you need to remove. I don't know about the ObjectiveC regex functions though, see Ron's post.

Upvotes: 1

Rahul Juyal
Rahul Juyal

Reputation: 2144

Try this code:

NSString *str = @"Turn left onto <a>Sri Krishna Nagar Rd</a><div class=\"google_note\">Pass by <b landmarkid=\"0x39ed58475c24020f:0x170a2130a5880d5a\" class=\"dir-landmark\">California Academy of Visual Effects</b> <div class=\"dirseg-sub\">(on the left)</div>\n</div>";
str = [str stringByReplacingOccurrencesOfString:@"\\r\\n" withString:@""];
NSRange r;
while ((r = [str rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
    str = [str stringByReplacingCharactersInRange:r withString:@""];
NSLog(@"%@",str);

Upvotes: 0

Related Questions