<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<SCRIPT LANGUAGE="JavaScript"> 
  String.prototype.Trimheadfoot = function() {
     return this.replace(/(^\s*)|(\s*$)/g,"");
  } 
  String.prototype.Trimmiddle = function() { 
    return this.replace(/(\s)\1+/g, "$1").replace(/(\r\n)\1+/g, "$1");
 }
//--> 
</SCRIPT> </head>
<body>
  <textarea cols="50" rows="5" onblur="this.value = this.value.Trimheadfoot().Trimmiddle()"></textarea>
  <input type="button" value="计算字数">
</body>
</html>

解决方案 »

  1.   


    str.replace(/\n{2,}/g,"\n").replase(/\s{2,}/g," ");
      

  2.   

    <script>
    String.prototype.Trimheadfoot = function() { return this.replace(/(^\s*)|(\s*$)/g,""); } 
    String.prototype.Trimmiddle = function() { return this.replace(/\s{2,}|[\r\n]{2,}/g, "$1"); }
    </script>
      

  3.   

    <script> 
      String.prototype.Trimheadfoot = function() { 
        return this.replace(/(^\s*)|(\s*$)/g,"");
      } 
      String.prototype.Trimmiddle = function() {
        return this.replace(/((?:\r\n)|\s)\1+/g, "$1"); 
      }
    </script>
      

  4.   

    这样应该够简洁了吧,换行支持\r\n或\n
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    <body>
    <script type="text/javascript">
    var str = "abc     abc\r\n\r\n\r\nabc     abc";
    alert(str);
    str = str.replace(/(\s)(\r*\1)+/g,"$1");
    alert(str);
    </script>
    </body>
    </html>
      

  5.   

    不小心案发现场错了一个字母。
    var str =" abd   def 123 \n\n xyj\n\nkkkl adfl";
    alert(str);
    alert(str.replace(/\n{2,}/g,"\n").replace(/[ ]{2,}/g," "));
      

  6.   

    <script> 
    String.prototype.Trimheadfoot = function() { return this.replace(/(^\s*)|(\s*$)/g,""); } 
    String.prototype.Trimmiddle = function() { return this.replace(/(\s|\r\n)\1+/gi, "$1"); } 
    </script> 
    <textarea cols="50" rows="5" onblur="this.value = this.value.Trimheadfoot().Trimmiddle()"></textarea>
      <input type="button" value="计算字数">
      

  7.   

    发现一个问题:当遇到回车后再打空格,就会出bug,变得不符合需求
      

  8.   

    String.prototype.Trim = function(c) {
        if(!c || c==' '){
            return this.replace(/(^\s*)|(\s*$)/g, "");
        }else{
            var s = this;
            var i, b = 0, e = s.length;
            for(i=0;i<s.length;i++)
            if(s.charAt(i) != c){ b=i; break; }  
            if(i==s.length)return  "";
            for(i = s.length-1;i>b;i--)
            if(s.charAt(i) != c){ e=i; break; }
            return s.substring(b,e+1);
        }
    }
    接受传入参数,相当于C#中的Trim
      

  9.   

    刚刚的还是有问题。
    再发一个。var str =" abd   def 123 \r\n\r\n xyj\r\n\nkkkl adfl";
    alert(str);
    alert(str.replace(/[ ]{2,}/g," ").replace(/\s{2,}/g,"\n"));
      

  10.   

    13楼的代码和3楼效果一样,且更简短,但是
    各位大大出bug了!!!!!
    bug:当遇到回车后再打空格,就会出bug,变得不符合需求
      

  11.   


    没问题啊,你没试过我写的么?
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    <body>
    <textarea id="txt" style="widht:400px;height:400px;"></textarea>
    <script type="text/javascript">
    var txt = document.getElementById("txt");
    var str = "abc     abc\n\n\n      abc     abc";
    txt.value = "替换之前:\r\n" + str + "\r\n";//替换
    str = str.replace(/(\s)(\r*\1)+/g,"$1");
    txt.value = txt.value + "替换之后:\r\n" + str;
    </script>
    </body>
    </html>
      

  12.   

    忘掉去首尾的空格了。var str ="    abd   def 123 \r\n\r\n xyj\r\n\nkkkl adfl    ";
    alert(str);
    alert(str.replace(/^\s+|\s+$/g,"").replace(/[ ]{2,}/g," ").replace(/\s{2,}/g,"\n"));
      

  13.   

    20楼,请用String.prototype来实现好吗?这样我嵌入代码及调试就很方便,谢谢
      

  14.   

    写成String.prototype了,呵呵
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    <body>
    <textarea id="txt" style="widht:400px;height:400px;"></textarea>
    <script type="text/javascript">
    //写成函数了
    String.prototype.Trim = function() { 
        return this.replace(/([\r\n ]{2,})/g,function(a){return a.indexOf("\n")>=0?"\r\n":" "});
    } //测试代码
    var txt = document.getElementById("txt");
    var str = "abc     abc\n\n\n      abc     abc";
    txt.value = "替换之前:\r\n" + str + "\r\n";
    str = str.Trim();
    txt.value = txt.value + "替换之后:\r\n" + str;
    </script>
    </body>
    </html>
      

  15.   

    呵呵,LZ的可以解决LZ就用吧
      

  16.   

    呵呵,LS的可以解决LZ就用吧
      

  17.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>无标题文档</title>
    </head>
    <body>
    <textarea id="txt" style="widht:400px;height:400px;"></textarea>
    <script type="text/javascript">
    //写成函数了
    String.prototype.Trim = function() { 
        return this.replace(/[ ]{2,}/g," ").replace(/(\s|[ ]){2,}/g,"\n");
    } //测试代码
    var txt = document.getElementById("txt");
    var str = "abc     abc\n\n\n  \n\r adsflkj  \n\r  \n\r    abc     abc";
    txt.value = "替换之前:\r\n" + str + "\r\n";
    str = str.Trim();
    txt.value = txt.value + "替换之后:\r\n" + str;
    </script>
    </body>
    </html>
      

  18.   

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title></title>
    <SCRIPT LANGUAGE="JavaScript"> 
    String.prototype.Trimheadfoot = function() { 
      return this.replace(/(^\s*)|(\s*$)/g,"");

    String.prototype.Trimmiddle = function() {
      return this.replace(/(?:(\r\n)|\s)\s+/g, "\r\n"); 
    }
    </SCRIPT> </head>
    <body>
    <textarea cols="50" rows="5" onblur="this.value = this.value.Trimheadfoot().Trimmiddle()"></textarea>
    <input type="button" value="计算字数">
    </body>
    </html>