title: Replace some text in a string and pass it to a view in SwiftUI
body: <p>I wish to convert some user input LaTeX string into HTML string. Since the replacement is some what complicated, like replacing <code>\textbf{...}</code> to <code>&lt;strong&gt;...&lt;/strong&gt;</code> and <code>\textit{...}</code> to <code>&lt;em&gt;...&lt;/em&gt;</code>, the <code>replacingOccurrences</code> method seems to be not enough for this purpose. Also, since the code for replacement is not directly related to UI drawing, I don't know where should one put this code.</p>
  <p>Below is the code, I stored the text that needed to process as the initial value of <code>inputText</code>.</p>
  <pre class="lang-swift prettyprint-override"><code>import SwiftUI
  
  struct LaTeXtoHTML: View {
      @State private var inputText: String =
          &quot;&quot;&quot;
          \\textbf{some{-}text}
          \\textit{other\{-\}text}
          ${a}$ \[{b}\]
          &quot;&quot;&quot;
      
      var body: some View {
          VStack {
              TextEditor(text: $inputText)
              Text(inputText)
              Spacer()
          }
      }
  }
  
  struct LaTeXtoHTML_Previews: PreviewProvider {
      static var previews: some View {
          LaTeXtoHTML()
      }
  }
  </code></pre>
  <p>The desired output is</p>
  <pre class="lang-html prettyprint-override"><code>&lt;strong&gt;some-text&lt;/strong&gt;
  &lt;em&gt;other{-}text&lt;/em&gt;
  ${a}$ \[{b}\]
  </code></pre>
  <p>Basically, the rules are</p>
  <p>(1) <code>\textbf{...}</code> converts to <code>&lt;strong&gt;...&lt;/strong&gt;</code>, <code>\textit{...}</code> to <code>&lt;em&gt;...&lt;/em&gt;</code>;</p>
  <p>(2) <code>{</code> and <code>}</code> are gone, but <code>\{</code> and <code>\}</code> is preserved and shown respectively as <code>{</code>, <code>}</code>;</p>
  <p>(3) Everything inside <code>$...$</code> or <code>\[...\]</code> is left untouched.</p>
  <p>You don't have to achieve all these for me, some hint would suffice. Thanks in advance for any kind of help!</p>