replace出现之间的string

我有一个string,我想要显示在uiwebview里面,看起来像这样:

"blah blah blah [IMG]you.png[/IMG] blah blah [IMG]me.png[/IMG]" 

我想把它换成这样:

 "blah blah blah <img src=\"you.png\"> blah blah <img src=\"me.png\">" 

我已经可以获得[IMG] … [/ IMG]之间的string,问题是它只能得到[IMG] … [/ IMG]的第一个集合,其余的事件不能。

 NSRange startRange = [finalQuestionString rangeOfString:@"[IMG]"]; if (startRange.location != NSNotFound) { NSRange targetRange; targetRange.location = startRange.location + startRange.length; targetRange.length = [finalQuestionString length] - targetRange.location; NSRange endRange = [finalQuestionString rangeOfString:@"[/IMG]" options:0 range:targetRange]; if (endRange.location != NSNotFound) { targetRange.length = endRange.location - targetRange.location; NSString *imageFile = [finalQuestionString substringWithRange:targetRange];//the extracted filename } } 

所以我怎么能得到[IMG] … [/ IMG]的所有事件,并将其replace

 "<img src=\"file.png\">" 

任何帮助,将不胜感激。 谢谢!

由于[IMG]永远会变成<img src=\"[/IMG]将永远变成\">" ,为什么不这样做呢:

 NSString *originalString = @"blah blah blah [IMG]you.png[/IMG] blah blah [IMG]me.png[/IMG]"; NSString *tempString = [originalString stringByReplacingOccurrencesOfString:@"[IMG]" withString:@"<img src=\""]; NSString *finalString = [tempString stringByReplacingOccurrencesOfString:@"\"[/IMG]" withString:@"\'>'"]; 

内存pipe理留给读者来做一个练习:)