JavaScript trim 实现去除字符串首尾指定字符的简单方法
String.prototype.trim = function (char, type) { if (char) { if (type == 'left') { return this.replace(new RegExp('^\'+char+'+', 'g'), ''); } else if (type == 'right') { return this.replace(new RegExp('\'+char+'+$', 'g'), ''); } return this.replace(new RegExp('^\'+char+'+|\'+char+'+$', 'g'), ''); } return this.replace(/^s+|s+$/g, ''); };