Ios extracts the img address (image address) from the html string ),
Original article address http://www.cnblogs.com/qianLL/p/6082287.html
Sometimes the background returns an html string. We need to extract the image address from it. This is the correct regular expression.
That is
<(Img | IMG )(.*?) (/> |> </Img> |>)
The code below returns all the image addresses in the string, all of which are a set.
+ (NSArray *)filterImage:(NSString *)html{ NSMutableArray *resultArray = [NSMutableArray array]; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<(img|IMG)(.*?)(/>|></img>|>)" options:NSRegularExpressionAllowCommentsAndWhitespace error:nil]; NSArray *result = [regex matchesInString:html options:NSMatchingReportCompletion range:NSMakeRange(0, html.length)]; for (NSTextCheckingResult *item in result) { NSString *imgHtml = [html substringWithRange:[item rangeAtIndex:0]]; NSArray *tmpArray = nil; if ([imgHtml rangeOfString:@"src=\""].location != NSNotFound) { tmpArray = [imgHtml componentsSeparatedByString:@"src=\""]; } else if ([imgHtml rangeOfString:@"src="].location != NSNotFound) { tmpArray = [imgHtml componentsSeparatedByString:@"src="]; } if (tmpArray.count >= 2) { NSString *src = tmpArray[1]; NSUInteger loc = [src rangeOfString:@"\""].location; if (loc != NSNotFound) { src = [src substringToIndex:loc]; [resultArray addObject:src]; } } } return resultArray;}