js删除字符串两端空格代码实例:
在很多其他语言中都有类似trim()的这么一个方法,它可以删除字符串两端的空格。
但是在javascript有点不同,不少朋友说javascript并没有这样的方法,其实这是错误的。
在ES5版本中已经新增了trim()方法,但是存在一定的浏览器兼容性问题,下面就是一段能够兼容所有浏览器的代码。
String.prototype.Trim = function(){ return this.replace(/^\s+/g,"").replace(/\s+$/g,""); } var str="softwhy.com "; console.log(str.length); console.log(str.Trim().length);
原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=17418
更多内容可以参阅:http://www.softwhy.com/zhengze/