如何在NSString中捕获特殊指示的**字符**并加粗它们之间的内容?

我无法在指定的一对“**”字符之间加粗任何字符。 例如,在此NSString中:

"The Fox has ran **around** the corner." 

应该读到:“狐狸走到了拐角处”

这是我的代码:

 NSString *questionString = queryString; NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:questionString]; NSRange range = [questionString rangeOfString:@"\\*([^**]+)\\*" options:NSRegularExpressionSearch|NSCaseInsensitiveSearch]; if (range.location != NSNotFound) { [mutableAttributedString setAttributes:@{NSFontAttributeName:[UIFont fontWithName:AGHeavyFontName size:size]} range:range]; } [[mutableAttributedString mutableString] replaceOccurrencesOfString:@"*" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, queryString.length)]; return mutableAttributedString; 

我有问题 – 这段代码仍然会用一对“*”来捕捉字符,所以在这种情况下,

  "The fox has ran *around the corner* 

仍然会读作“狐狸已经跑到拐角处”,当它不应该时。

有任何想法吗?

也许这可以帮助你:

http://regex101.com/r/eF6pJ8

\\*{2}([^*]*)\\*{2}