目标C - 摆脱<em>和</em>

时间:2015-12-23 20:35:25

标签: objective-c

我有以下字符串:

@"Hello there, this is <em> a string. </em> Here's another scenario with tags, for example, <em> this </em>."

我需要删除<em></em>标记之间的所有内容,包括标记,因此结果应为:

@"Hello there, this is Here's another scenario with tags, for example, ."

如何使用正则表达式完成此操作?

1 个答案:

答案 0 :(得分:3)

使用以下方法:

- (NSString *)stringByStrippingEMTag:(NSString *)str {
    NSRange r;
    NSString *s = [str copy];
    while ((r = [s rangeOfString:@"<em[^>]*>(.*?)</em>" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@""];
    } 
    return s;
}