title: Check a string contain how many word in javascript
body: <p>I would like to check how many words in a string</p>
  
  <p>eg.</p>
  
  <pre><code> asdsd sdsds sdds 
  </code></pre>
  
  <p>3 words</p>
  
  <p>The problem is , if there is more than one space between two substring , the result is not correct</p>
  
  <p>here is my program</p>
  
  <pre><code>function trim(s) {   
    return s.replace(/^\s*|\s*$/g,"")   
  } 
  
  var str = trim(x[0].value);
  var parts = str .split(" ");
  alert (parts.length);
  </code></pre>
  
  <p>How to fix the problem? thanks for help</p>