title: Stop Nokogiri from removing new lines
body: <p>I'm trying to strip out all html from a string so I can perform some regexes on it. I discovered you can use Nokogiri to get the text by using xpath like so:</p>
  
  <pre><code>Nokogiri::HTML(my_string).xpath("//text()").text
  </code></pre>
  
  <p>This works quite well, but I have found that it removes some new line characters almost randomly, which ends up breaking my regular expressions. The html it receives is always valid (although often not well formed - it was made using a wysiwyg editor) so that shouldn't be an issue.</p>
  
  <p>Is there a reason that Nokogiri would remove the new lines and is there a way to avoid it? This is for a command line script, so I don't have rails or anything else.</p>
  
  <p>here's a sample of the text where it's not preserving newlines:</p>
  
  <pre><code>&lt;p&gt;Composition: &lt;span&gt;83% Polyester Bright, 17% Spandex &amp;nbsp; &amp;nbsp;&lt;/span&gt;&amp;nbsp;&lt;span&gt;&lt;br /&gt;
   &lt;/span&gt;Washing: Cold hand wash only&lt;span&gt;&lt;br /&gt;
   &lt;/span&gt;Designer: James Lillis&amp;nbsp;&lt;span&gt;&lt;br /&gt;
   &lt;/span&gt;Made in: Australia&amp;nbsp;&lt;/p&gt;
  </code></pre>
  
  <p>If it matters, I am reading the html in from a JSON file using Oj. This is how it appears in the JSON file:</p>
  
  <pre><code>"\u003Cp\u003EComposition: \u003Cspan\u003E83% Polyester Bright, 17% Spandex \u0026nbsp; \u0026nbsp;\u003C/span\u003E\u0026nbsp;\u003Cspan\u003E\u003Cbr /\u003E\n \u003C/span\u003EWashing: Cold hand wash only\u003Cspan\u003E\u003Cbr /\u003E\n \u003C/span\u003EDesigner: James Lillis\u0026nbsp;\u003Cspan\u003E\u003Cbr /\u003E\n \u003C/span\u003EMade in: Australia\u0026nbsp;\u003C/p\u003E\n"
  </code></pre>
  
  <p>And this is how it ends up when i put it through the above nokogiri function call:</p>
  
  <pre><code>Composition: 83% Polyester Bright, 17% Spandex     Washing: Cold hand wash onlyDesigner: James Lillis Made in: Australia
  </code></pre>
  
  <p>Thanks for any help</p>
  
  <p><strong>UPDATE</strong>: I've discovered that it appears to remove the newlines when the <code>\n</code> character is in a span element (which for some reason, the editor did a lot). If there is a way to stop this from happening, I'd like to know, otherwise I'll probably try to detect it beforehand. </p>