写两个方法,返回字符串中的空格数,和回车数,用prototype实现。String.prototype.returnSpace = function(){return '所有空格数'}
String.prototype.returnEnter = function(){return '所有回车数'}各位高手帮忙,谢谢
String.prototype.returnEnter = function(){return '所有回车数'}各位高手帮忙,谢谢
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());
alert(''.returnEnter());
应返回0,现报错,解决
String.prototype.returnEnter = function(){return this==''?0:this.match(/\n/g).length;}
alert(' sdfsd sdf sdflsd f'.returnSpace());
alert(''.returnEnter());