title: Replacing &nbsp; from javascript dom text node
body: <p>I am processing xhtml using javascript. I am getting the text content for a div node by concatenating the nodeValue of all child nodes where nodeType == Node.TEXT_NODE.</p>
  
  <p>The resulting string sometimes contains a non-breaking space entity. How do I replace this with a regular space character?</p>
  
  <p>My div looks like this...</p>
  
  <blockquote>
    <p><code>&lt;div&gt;&lt;b&gt;Expires On&lt;/b&gt; Sep 30, 2009 06:30&amp;nbsp;AM&lt;/div&gt;</code></p>
  </blockquote>
  
  <p>The following suggestions found on the web did not work:</p>
  
  <pre><code>var cleanText = text.replace(/^\xa0*([^\xa0]*)\xa0*$/g,"");
  
  
  var cleanText = replaceHtmlEntities(text);
  
  var replaceHtmlEntites = (function() {
    var translate_re = /&amp;(nbsp|amp|quot|lt|gt);/g;
    var translate = {
      "nbsp": " ",
      "amp" : "&amp;",
      "quot": "\"",
      "lt"  : "&lt;",
      "gt"  : "&gt;"
    };
    return function(s) {
      return ( s.replace(translate_re, function(match, entity) {
        return translate[entity];
      }) );
    }
  })();
  </code></pre>
  
  <p>Any suggestions?</p>