Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
NSString: Get all matching NSRange of a substring
- (NSArray *)rangesInSubstring:(NSString *)substring
NSError *error = NULL;
NSString *regex = [NSString stringWithFormat:@"\\b%@", substring];
NSRegularExpression *regExpression = [NSRegularExpression regularExpressionWithPattern:regex options:NSRegularExpressionCaseInsensitive error:&error];
NSMutableArray *ranges = [NSMutableArray array];
NSArray *matches = [regExpression matchesInString:self options:NSRegularExpressionSearch range:NSMakeRange(0, self.length)];
for (NSTextCheckingResult *match in matches) {
[ranges addObject:[NSValue valueWithRange:match.range]];
return ranges;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.