title: Splitting a string into sections based on headers
body: <p>I want to split a string into sections based on headers which follow a specific format (<code>==HEADER==</code>). Here's what the input string would look like:</p>
  
  <pre><code>== Section header ==
  Text inside section
  === Maybe a nested section ===
  With some more text
  And more text
  ==Then the next section header, perhaps w/o spaces between text and equals signs==
  With text inside it
  </code></pre>
  
  <p>Here's the output I'd like:</p>
  
  <pre><code>[
      '== Section header ==
      Text inside section
      === Maybe a nested section ===
      With some more text
      And more text',
      '==Then the next section header, without spaces between text and equals signs==
      With text inside it'
  ]
  </code></pre>
  
  <p>I tried doing</p>
  
  <pre><code>pagetext = "== Test header ==\n Some test text, with random equals signs==newlines\n or whatever\n ==Another header    ==   \n more text,\nnewlines\nohmy"
  sections = [];
  section_re = /==\s*(\s*[^=]*)\s*==/g;
  var section_headers = pagetext.match(section_re);
  for (var i = 0; i &lt; section_headers.length; i++) {
      var section_start = pagetext.indexOf(section_headers[i]);
      var section_text = pagetext.substring(section_start);
      if (i &lt; section_headers.length - 1) {
          var section_end = section_text.substring(section_headers[i].length).indexOf(section_headers[i + 1]) + section_headers[i].length;
          section_text = section_text.substring(0, section_end);
      }
      sections.push(section_text);
  }
  </code></pre>
  
  <p>But it split on the "random equals" signs, giving me:</p>
  
  <pre><code> ["== Test header ==\n Some...ith random equals signs", "==newlines\n or whatever...ore text,\nnewlines\nohmy"]
  </code></pre>
  
  <p>This isn't right. I have a feeling my code may just be too complex -- is there a better way to do this?</p>