解决方案 »

  1.   


    var spaceReg = "\\s+";
      

  2.   

    <script>
    var str = "<div><p>        hello    word.   this                 is            tom.</p><p>    hello      word.    this is jack.</p></div>";
    console.log(str.replace(/\s+/g,' '));//<div><p> hello word. this is tom.</p><p> hello word. this is jack.</p></div>
    </script>
      

  3.   

    var str = "<div><p>        hello    word.   this                 is            tom.</p><p>    hello      word.    this is jack.</p></div>";
    console.log(str.match(/\s+/) + str.replace(/\s+/g, ' '));保留最前空格+移除所有空格
      

  4.   


        var a=$("input").val();
        a=a.replace(/[ ]/g,"");
        alert(a);