1.呕心沥血之作
2.字符串操作
2-1去除字符串空格
//去除空格 type 1-所有空格 2-前后空格 3-前空格 4-后空格
function trim(str,type){
switch (type){
case 1:return str.replace(/\s+/g,”“);
case 2:return str.replace(/(^\s*)|(\s*
)/g,“”);case3:returnstr.replace(/(\s∗)/g,“”);case4:returnstr.replace(/(\s∗
)/g, “”);
default:return str;
}
}
2-2字母大小写切换
/*type
1:首字母大写
2:首页母小写
3:大小写转换
4:全部大写
5:全部小写
* */
//changeCase(‘asdasd’,1)
//Asdasd
function changeCase(str,type)
{
function ToggleCase(str) {
var itemText = “”
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(/^(\w)(\w+)/, function (v, v1, v2) {
return v1.toUpperCase() + v2.toLowerCase();
});
case 2:
return str.replace(/^(\w)(\w+)/, function (v, v1, v2) {
return v1.toLowerCase() + v2.toUpperCase();
});
case 3:
return ToggleCase(str);
case 4:
return str.toUpperCase();
case 5:
return str.toLowerCase();
default:
return str;
}
}
2-3字符串循环复制
//repeatStr(str->字符串, count->次数)
//repeatStr(‘123’,3)
//”123123123”
function repeatStr(str, count) {
var text = ”;
for (var i = 0; i < count; i++) {
text += str;
}
return text;
}
2-4字符串替换
//字符串替换(字符串,要替换的字符,替换成什么)
function replaceAll(str,AFindText,ARepText){
raRegExp = new RegExp(AFindText,”g”);
return str.replace(raRegExp,ARepText);
}
2-5替换*
//replaceStr(字符串,字符格式, 替换方式,替换的字符(默认*))
function replaceStr(str, regArr, type,ARepText) {
var regtext = ”, Reg = null,replaceText=ARepText||’*’;
//replaceStr(‘18819322663’,[3,5,3],0)
//188*****663
//repeatStr是在上面定义过的(字符串循环复制),
if (regArr.length === 3 && type === 0) {
regtext = ‘(\w{’ + regArr[0] + ‘})\w{’ + regArr[1] + ‘}(\w{’ + regArr[2] + ‘})’
Reg = new RegExp(regtext);
var replaceCount = repeatStr(replaceText, regArr[1]);
return str.replace(Reg, ‘
1′+replaceCount+′
2’)
}
//replaceStr(‘asdasdasdaa’,[3,5,3],1)
//asdas
else if (regArr.length === 3 && type === 1) {
regtext = ‘\w{’ + regArr[0] + ‘}(\w{’ + regArr[1] + ‘})\w{’ + regArr[2] + ‘}’
Reg = new RegExp(regtext);
var replaceCount1 = repeatSte(replaceText, regArr[0]);
var replaceCount2 = repeatSte(replaceText, regArr[2]);
return str.replace(Reg, replaceCount1 + ‘1’ + replaceCount2)
}
//replaceStr(‘1asd88465asdwqe3’,[5],0)
//*****8465asdwqe3
else if (regArr.length === 1 && type == 0) {
regtext = ‘(^\w{’ + regArr[0] + ‘})’
Reg = new RegExp(regtext);
var replaceCount = repeatSte(replaceText, regArr[0]);
return str.replace(Reg, replaceCount)
}
//replaceStr(‘1asd88465asdwqe3’,[5],1,’+’)
//”1asd88465as+++++”
else if (regArr.length === 1 && type == 1) {
regtext = ‘(\w{’ + regArr[0] + ‘})’
Reg = new RegExp(regtext);
var replaceCount = repeatSte(replaceText, regArr[0]);
return str.replace(Reg, replaceCount)
}
}
2-6检测字符串
//checkType(‘165226226326’,’phone’)
//false
function checkType (str, type) {
switch (type) {
case ‘email’:
return /^[\w-]+(.[\w-]+)*@[\w-]+(.[\w-]+)+
/.test(str);case‘phone′:return/1[3|4|5|7|8][0−9]9
/.test(str);
case ‘tel’:
return /^(0\d{2,3}-\d{7,8})(-\d{1,4})?
/.test(str);case‘number′:return/[0−9]
/.test(str);
case ‘english’:
return /^[a-zA-Z]+
/.test(str);case‘chinese′:return/[\u4E00−\u9FA5]+
/.test(str);
case ‘lower’:
return /^[a-z]+
/.test(str);case‘upper′:return/[A−Z]+
/.test(str);
default :
return true;
}
}
2-7检测密码强度
//checkPwd(‘12asdASAD’)
//3(强度等级为3)
function checkPwd(str) {
var nowLv = 0;
if (str.length < 6) {
return nowLv
}
if (/[0-9]/.test(str)) {
nowLv++
}
if (/[a-z]/.test(str)) {
nowLv++
}
if (/[A-Z]/.test(str)) {
nowLv++
}
if (/[.|-|_]/.test(str)) {
nowLv++
}
return nowLv;
}
2-8随机码
//count取值范围0-36
//randomNumber(10)
//”2584316588472575”
//randomNumber(14)
//”9b405070dd00122640c192caab84537”
//Math.random().toString(36).substring(2);
//”83vhdx10rmjkyb9”
function randomNumber(count){
return Math.random().toString(count).substring(2);
}
2-9查找字符串
可能标题会有点误导,下面我就简单说明一个需求,在字符串’sad44654blog5a1sd67as9dablog4s5d16zxc4sdweasjkblogwqepaskdkblogahseiuadbhjcibloguyeajzxkcabloguyiwezxc967’中找出’blog’的出现次数。代码如下
function countStr (str,strSplit){
return str.split(strSplit).length-1
}
var strTest=’sad44654blog5a1sd67as9dablog4s5d16zxc4sdweasjkblogwqepaskdkblogahseiuadbhjcibloguyeajzxkcabloguyiwezxc967’
//countStr(strTest,’blog’)
//6
3.数组操作
3-1数组去重
//ES6新增的Set数据结构,类似于数组,但是里面的元素都是唯一的 ,其构造函数可以接受一个数组作为参数
//let arr=[1,2,1,2,6,3,5,69,66,7,2,1,4,3,6,8,9663,8]
//let set = new Set(array);
//{1,2,6,3,5,69,66,7,4,8,9663}
//ES6中Array新增了一个静态方法from,可以把类似数组的对象转换为数组
//Array.from(set)
//[1,2,6,3,5,69,66,7,4,8,9663]
function removeRepeatArray(arr){
return Array.from(new Set(arr))
}
3-2数组顺序打乱
function upsetArr(arr){
return arr.sort(function(){ return Math.random() - 0.5});
}
3-3数组最大值最小值
//这一块的封装,主要是针对数字类型的数组
function maxArr(arr){
return Math.max.apply(null,arr);
}
function minArr(arr){
return Math.min.apply(null,arr);
}
3-4数组求和,平均值
//这一块的封装,主要是针对数字类型的数组
//求和
function sumArr(arr){
var sumText=0;
for(var i=0,len=arr.length;i