     var bracketDepth = 0
 
     while let firstChar = string.characters.first {
-      if string.hasPrefix("]]") && bracketDepth == 0 {
+      if string.hasPrefix(terminator) && bracketDepth == 0 {
         return offset
       }
       if firstChar == "\\" {

        regExPattern += "("
        curParen += 1

        let substr = patternStr.substring(
          with: Range<String.Index>(
            uncheckedBounds: (
              patternStr.index(patternStr.startIndex, offsetBy: 2),
              End.lowerBound
            )
          )
        )
        let (res, paren) = self.addRegExToRegEx(substr, curParen)
        curParen = paren
        if res {

      // RegEx matches.
      if patternStr.range(of: "{{")?.lowerBound == patternStr.startIndex {
        // This is the start of a regex match.  Scan for the }}.
		patternStr = patternStr.substring(from: patternStr.index(patternStr.startIndex, offsetBy: 2))
        guard let end = self.findRegexVarEnd(patternStr, brackets: (open: "{", close: "}"), terminator: "}}") else {
          let loc = CheckLoc.inBuffer(pattern.baseAddress!, buf)
          diagnose(.error, loc, "found start of regex string with no end '}}'")
          return true
