我正在尝试检测星号之间的任何单词:
NSString *questionString = @"hello *world*";
NSMutableAttributedString *goodText = [[NSMutableAttributedString alloc] initWithString:questionString]; //should turn the word "world" blue
NSRange range = [questionString rangeOfString:@"\\b\\*(.+?)\\*\\b" options:NSRegularExpressionSearch|NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
DLog(@"found a word within asterisks - this never happens");
[goodText addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:range];
}
但我从来没有得到积极的结果。正则表达式有什么问题?