title: Split a sentence with HTML into words (but leave inline HTML intact)
body: <p>I am looking for a way to use javascript for splitting a sentence with HTML into words, and leaving the inline HTML tags with the text content intact. Punctuation can be regarded as a part of the word it is closest to. I'd like to use regex, and probably <code>preg_split()</code> for splitting the sentences. Here follows an example:</p>
  
  <pre><code>A word, &lt;a href='#' title=''&gt;words within tags should remain intact&lt;/a&gt;, so here's
  &lt;b&gt;even more&lt;/b&gt; &lt;u&gt;words&lt;/u&gt;
  </code></pre>
  
  <p>Preferably, I would like to end up with the following:</p>
  
  <pre><code>[0] =&gt; A
  [1] =&gt; word,
  [2] =&gt; &lt;a href='#' title=''&gt;words within tags should remain intact&lt;/a&gt;,
  [3] =&gt; so
  [4] =&gt; here's
  [5] =&gt; &lt;b&gt;even more&lt;/b&gt;
  [6] =&gt; &lt;u&gt;words&lt;/u&gt;
  </code></pre>
  
  <hr>
  
  <p>I know about the discussion on parsing HTML with Regex (I enjoyed reading <a href="https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454">Bobince' answer</a> :-P ), but I need to split the words of a sentence without harming html-tags with attributes. I don't see how I can do this with JS in a different way than Regex. Of course, if there are alternatives, I'd be more than happy to adapt them, to achieve a similar result. :-)</p>
  
  <hr>
  
  <p><strong>Edit:</strong>
  I searched for similar questions on Stackoverflow about this, but these don't tick the boxes for me. To put it a little into perspective:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/1693396/splitting-up-html-code-tags-and-content">splitting-up-html-code-tags-and-content</a>: targets to split up the inline HTML, which is what I want to leave intact.</li>
  <li><a href="https://stackoverflow.com/questions/7891771/php-regex-to-match-outside-of-html-tags">php-regex-to-match-outside-of-html-tags</a>: targets all text nodes in a HTML snippet, even within HTML tags. But as a matter of fact, I want to target only the spaces outside of HTML elements (so even excluding the spaces within the text nodes being wrapped with HTML tags).</li>
  </ul>