生活随笔
收集整理的這篇文章主要介紹了
前端:JS字符串操作函数类库
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
? ? ??
1、字符串去掉空格
/*** 去除空格* @param {str}* @param {type}*type: 1-所有空格 2-前后空格 3-前空格 4-后空格* @return {String}*/
trim (str, type) {type = type || 1switch (type) {case 1:return str.replace(/\s+/g, "");case 2:return str.replace(/(^\s*)|(\s*$)/g, "");case 3:return str.replace(/(^\s*)/g, "");case 4:return str.replace(/(\s*$)/g, "");default:return str;}
}2、大小寫轉(zhuǎn)換
/*** @param {str}* @param {type}* type:1:首字母大寫2:首字母小寫3:大小寫轉(zhuǎn)換4:全大寫5:全小寫* @return {String}*/
changeStrCase (str, type) {type = type || 4switch (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 str.split('').map( function(word){if (/[a-z]/.test(word)) {return word.toUpperCase();}else{return word.toLowerCase()}}).join('')case 4:return str.toUpperCase();case 5:return str.toLowerCase();default:return str;}
}
3、檢測密碼強度
/*檢測密碼強度
*/
checkPwd (str) {var Lv = 0;if (str.length < 6) {return Lv}if (/[0-9]/.test(str)) {Lv++}if (/[a-z]/.test(str)) {Lv++}if (/[A-Z]/.test(str)) {Lv++}if (/[\.|-|_]/.test(str)) {Lv++}return Lv;
}
4、過濾html代碼/*過濾html代碼*/
filterTag (str) {str = str.replace(/&/ig, "&");str = str.replace(/</ig, "<");str = str.replace(/>/ig, ">");str = str.replace(" ", " ");return str;
}
文章推薦程序員效率:畫流程圖常用的工具程序員效率:整理常用的在線筆記軟件遠程辦公:常用的遠程協(xié)助軟件,你都知道嗎?51單片機程序下載、ISP及串口基礎(chǔ)知識硬件:斷路器、接觸器、繼電器基礎(chǔ)知識
總結(jié)
以上是生活随笔為你收集整理的前端:JS字符串操作函数类库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。