1、substring()截取从第一个非空格字符的索引到最后一个非空格字符索引之间的所有字符,返回截取后的字符串
String.prototype.trimOne = function () {
return this.substring(Math.max(this.search(/\S/), 0), this.search(/\S\s*$/)+1)
};
2、 replace()方法,将字符串开头的所有空格用一个空字符串代替,再将字符床尾部的所有空格用一个空字符串替代
String.prototype.trimTwo = function () {
return this.replace(/^\s+/, '').replace(/\s+$/, '');
};
3、对 trimTwo方法的简化,看起来更加优雅的写法
String.prototype.trimThree = function () {
return this.replace(/^\s+|\s+$/g, '')
};