I have a string, for example "F (R U R' U') (R U R' U') (R U R' U') F'"
. I am using NSAttributedString to search for the text in brackets (R U R' U')
and replace it with the same text, just in a different colour. The code I'm using is
let mutableAttributedString = NSMutableAttributedString(string: algToShow)
var searchRange = NSRange(location: 0, length: algToShow.count)
var foundRange1 = NSRange()
foundRange1 = (algToShow as NSString).range(of: "(R U R' U')", options: NSString.CompareOptions.caseInsensitive, range: searchRange)
if foundRange1.location != NSNotFound || foundRange2.location != NSNotFound || foundRange3.location != NSNotFound || foundRange4.location != NSNotFound || foundRange5.location != NSNotFound {
// found an occurrence of the substring! do stuff here
searchRange.location = foundRange1.location + foundRange1.length
mutableAttributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: foundRange1)
However, it only highlights the first set of text/brackets; the others are ignored completely. The question is how can I check for how many times the brackets appear, and replace every instance of them?
Thanks.