Using Javascript to parse file with regex and convert info into json
<p>How can I organize the following data format into JSON? The data is a simple text file that looks like:</p>

<pre><code>&lt;--Header Title--&gt;
Some block of info here
&lt;--Header Title--&gt;
Some block of info here
&lt;--Header Title--&gt;
Some block of info here
</code></pre>

<p>There are some <strong>tricky bits</strong>, like:</p>

<p>a) Possible to contain <code>&lt;--Header Title--&gt;</code> inside the <code>Some block of info here</code>:</p>

<pre><code>&lt;--Header Title--&gt;
I am info for &lt;--Header Title--&gt;
&lt;--Header Title--&gt;
This &lt;--Header Title--&gt; is finished
&lt;--Header Title--&gt;
&lt;--Header Title--&gt; contains the following:
stuff1
stuff2
stuff3
</code></pre>

<p>b) <code>Some block of info here</code> can either be empty or just whitespaces/newlines</p>

<pre><code>&lt;--Header Title--&gt;
&lt;--Header Title--&gt;


&lt;--Header Title--&gt;
Info text here
</code></pre>

<p>c) <code>Some block of info here</code> format should be preserved, unless it's only whitespaces/newlines like in (b). So the following should preserve the leading and ending whitespaces/newlines:</p>

<pre><code>&lt;--Header Title--&gt;

More info about blah

blah blah blah
blah blah

&lt;--Header Title--&gt;
Another info about blah
</code></pre>

<p><strong>All in all, I'd like to convert this into a json for easy retrieval.</strong> A simple example:</p>

<pre><code>&lt;-- Option 1 --&gt;
Nice text
&lt;-- Option 2--&gt;


&lt;--Final stuff--&gt;
Listing all
of
the
text

&lt;--Header Title--&gt;
I am info for &lt;--Header Title--&gt;
&lt;--Header Title--&gt;
This &lt;--Header Title--&gt; is finished
&lt;--Header Title--&gt;
&lt;--Header Title--&gt; contains the following:
stuff1
stuff2
stuff3
</code></pre>

<p>json:</p>

<pre><code>{
  "data":
    [
        {"Option 1": "Nice text"},
        {"Option 2": ""},
        {"Final stuff": "Listing all\nof\nthe\ntext\n"},
        {"Header Title": "I am info for &lt;--Header Title--&gt;"},
        {"Header Title": "This &lt;--Header Title--&gt; is finished"},
        {"Header Title": "&lt;--Header Title--&gt; contains the following:\nstuff1\nstuff2\nstuff3"}
    ]
}
</code></pre>

<p>My current regex is:</p>

<pre><code>\&lt;\-\-(.*)\-\-\&gt;\n(.*)
</code></pre>

<p>But this only captures the first occurrence, and only if (a) doesn't occur.</p>