So I have an NSString which is basically an html string with all the usual html elements. The specific thing I would like to do is to just strip it from all the img tags.
The img tags may or may not have max-width, style or other attributes so I do not know their length up front. They always end with />
How could I do this?
EDIT: Based on nicolasthenoz‘s answer, I came up with a solution that requires less code:
NSString *HTMLTagss = @"<img[^>]*>"; //regex to remove img tag
NSString *stringWithoutImage = [htmlString stringByReplacingOccurrencesOfRegex:HTMLTagss withString:@""];
You can use the
NSStringmethodstringByReplacingOccurrencesOfStringwith theNSRegularExpressionSearchoption:Or you can also use the
replaceMatchesInStringmethod ofNSRegularExpression. Thus, assuming you have your html in aNSMutableString *html, you can:I’d personally lean towards one of these options over the
stringByReplacingOccurrencesOfRegexmethod ofRegexKitLite. There’s no need to introduce a third-party library for something as simple as this unless there was some other compelling issue.