title: How to use Regular Expression in Swift to replace matching expressions with the template string instead of plain text
body: <p>I have a project with a main textView and two textFields. In the main textView field there is  the text to process, in the first textField you enter a regular expression and in the second textField you enter the substitution template string. </p>
  
  <p>If in the second textField I enter text, all work fine, but if I enter some regex it seems that this is handled like plain text. For example entering \n\n instead of adding two newlines characters in the final text, it just enter nn.</p>
  
  <p>Here is my code, it's partially based on</p>
  
  <ul>
  <li>this answer by OOPer:
  <a href="https://stackoverflow.com/questions/38385539/how-to-check-in-swift-if-a-regular-expression-is-valid">How to check in Swift if a Regular Expression is valid?</a>
  and</li>
  <li>this answer by Wiktor Stribiżew:
  <a href="https://stackoverflow.com/questions/38375557/how-to-avoid-an-error-due-to-special-characters-in-search-string-when-using-rege">How to avoid an error due to special characters in search string when using Regex for a simple search in Swift?</a></li>
  </ul>
  
  <p>What am I doing wrong?</p>
  
  <pre><code>if let regex =  NSRegularExpression.expresssionWith(theSearchOptions){ // theSearchOptions uses the struct below
  
      // replace matches
      let regStr = regex.stringByReplacingMatchesInString(str, options: .WithTransparentBounds, range: NSMakeRange(0, str.characters.count), withTemplate: substitution) // EDIT: substitution is the var that contains the template string
      newStr = regStr
  }
  
  
  struct SearchOptions {
      let searchString: String
      var replacementString: String
      let matchCase: Bool
      let wholeWords: Bool
      let escapeMetacharacters: Bool
  }
  
  extension NSRegularExpression {
      static func expresssionWith(options: SearchOptions) -&gt; NSRegularExpression? {
          let searchString = options.searchString
          let isCaseSensitive = options.matchCase // true
          let isWholeWords = options.wholeWords // false
          let escapeMetacharacters = options.escapeMetacharacters // false
  
          // handle case sensitive option
          var regexOption: NSRegularExpressionOptions = .CaseInsensitive
          if isCaseSensitive { // if it is match case remove case sensitive option
              regexOption = []
          }
  
          // put the search string in the pattern
          var pattern = searchString
  
          if escapeMetacharacters {
              pattern = NSRegularExpression.escapedPatternForString(searchString)
              // adding backslash escapes as necessary to protect any characters that would match as pattern metacharacters
          } else {
              // no changes in the pattern
          }
  
          if isWholeWords {
              pattern = "(?&lt;!\\w)" + NSRegularExpression.escapedPatternForString(searchString) + "(?!\\w)"
          }
  
          do {
              return try NSRegularExpression(pattern: pattern, options: regexOption)
          } catch  {
              print("Error: \(error)")
              return nil
          }
      }
  }
  </code></pre>