写两个方法,返回字符串中的空格数,和回车数,用prototype实现。String.prototype.returnSpace = function(){return '所有空格数'}
String.prototype.returnEnter = function(){return '所有回车数'}各位高手帮忙,谢谢

解决方案 »

  1.   


    String.prototype.returnSpace = function(){return this.match(/\s/g).length;} 
    String.prototype.returnEnter = function(){return this.match(/\n/g).length;}
    alert('    sdfsd sdf sdflsd f'.returnSpace());
    alert('sdf\nsdf\n'.returnEnter());
      

  2.   

    alert(''.returnSpace());
    alert(''.returnEnter());
    应返回0,现报错,解决
      

  3.   

    大哥,那你稍微改下不就OK啦String.prototype.returnSpace = function(){return this==''?0:this.match(/\s/g).length;} 
    String.prototype.returnEnter = function(){return this==''?0:this.match(/\n/g).length;}
    alert('    sdfsd sdf sdflsd f'.returnSpace());
    alert(''.returnEnter());