Question

A have a little problem with parsing HTML into string. I have a big string and I need delete characters between "script" tag. Something like this :

    <script...>Some text here</script>

So I need to delete "Some text here". I think it would be great to use NSRegularExpression. Can anybody help me ? Thanks a lot.

Was it helpful?

Solution

While you will generally be advised to not parse HTML with regular expressions (see my all-time favorite S.O. answer), you can approximate it with something like:

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<script.*?>(.*?)</script>"
                                                                       options:NSRegularExpressionCaseInsensitive | NSRegularExpressionDotMatchesLineSeparators
                                                                         error:&error];

[regex enumerateMatchesInString:string
                        options:0
                          range:NSMakeRange(0, [string length])
                     usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {

                         NSLog(@"%@", [string substringWithRange:[result rangeAtIndex:1]]);
                     }];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top