首页 > 代码库 > ios-字符串替换-正则表达式-例子

ios-字符串替换-正则表达式-例子

需求:在html中查找并替换相应的html标签

代码实现

- (NSString *)replaceImageHtml:(NSString *)oldHtml {    NSString *regex = @"(<img.*?/>)";    NSRange r;    NSMutableString *newHtml = [NSMutableString stringWithString:oldHtml];        BOOL flag = false;        while (flag == false) {                r = [newHtml rangeOfString:regex options:NSRegularExpressionSearch];        if (r.location != NSNotFound) {            [newHtml replaceCharactersInRange:r withString:@""];        } else {            flag = true;        }            };    return newHtml;}