trim函数去除空格(所有空格,前后,前,后):
//value:要去除空格的字符串
//type: 1-所有空格 2-前后空格 3-前空格 4-后空格
function trim(value, type) {
switch (type) {
case 1:
return value.replace(/\s+/g, "");
case 2:
return value.replace(/(^\s*)|(\s*$)/g,"");
case 3:
return value.replace(/(^\s*)/g, "");
case 4:
return value.replace(/(\s*$)/g, "");
default:
return value;
}
}
字母大小写切换:
// type: 1:⾸字⺟⼤写
// 2:⾸字⺟⼩写
// 3:⼤⼩写转换
// 4:全部⼤写
// 5:全部⼩写
function changeCase(str, type) {
function ToggleCase(str) {
var itemText = ""
console.log('str.split("")',str.split(""))
str.split("").forEach(
function (item) {
if (/^([a-z]+)/.test(item)) {
itemText += item.toUpperCase();
} else if (/^([A-Z]+)/.test(item)) {
itemText += item.toLowerCase();
} else {
itemText += item;
}
});
return itemText;
}
switch (type) {
case 1:
return str.replace(/\b\w+\b/g, function(word) {
return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase();
});
case 2:
return str.replace(/\b\w+\b/g, function(word) {
return word.substring(0, 1).toLowerCase() + word.substring(1).toUpperCase();
});
case 3:
return ToggleCase(str);
case 4:
return str.toUpperCase();
case 5:
return str.toLowerCase();
default:
return str;
}
}