原生的trim()
只能去除首尾空格,为了更方便的处理一些拼接的数据 如: s;b;c;
,在String
原型上扩展了trims()
方法
/**
* 去除字符串 指定的首尾字符
* @param { String } code
* @return 去除后的字符串
* 示例: ';;;aaaa;;;'.trims(';') ==> 'aaaa'
*/
if(!String.prototype.trims) {
Object.defineProperty(String.prototype, 'trims', {
value: function (code) {
let str = this;
//匹配正则
let regs = new RegExp(`(^\\${code}*)|(\\${code}*$)`,'g')
str = str.replace(regs, "")
return str
}
})