/**
* 去除空格
* @param {string} str 字符串
* @param {string} type 去除方式
* @return string
*/
trim(str, type = 'both') {
if (type == 'both') {
//去除两端
return str.replace(/^\s+|\s+$/g, '');
} else if (type == "left") {
//去除左边
return str.replace(/^\s*/, '');
} else if (type == 'right') {
//去除右边
return str.replace(/(\s*$)/g, '');
} else if (type == 'all') {
//去除所有
return str.replace(/\s+/g, '');
} else {
return str;
}
}
JS - 去掉字符串中的空格
于 2022-09-15 17:45:06 首次发布