加入收藏 | 设为首页 | 会员中心 | 我要投稿 葫芦岛站长网 (https://www.0429zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > 建站资源 > 优化 > 正文

JavaScript常用工具方法封装

发布时间:2019-02-15 02:49:27 所属栏目:优化 来源:佚名
导读:JavaScript 1. type 类型判断 isString(o){//是否字符串 returnObject.prototype.toString.call(o).slice(8,-1)==='String' } isNumber(o){//是否数字 returnObject.prototype.toString.call(o).slice(8,-1)==='Number' } isBoolean(o){//是否boolean retu

4. String 字符串操作

  1. /** 
  2.  * 去除空格 
  3.  * @param  {str} 
  4.  * @param  {type}  
  5.  *       type:  1-所有空格  2-前后空格  3-前空格 4-后空格 
  6.  * @return {String} 
  7.  */ 
  8. trim (str, type) { 
  9.     type = type || 1 
  10.     switch (type) { 
  11.         case 1: 
  12.             return str.replace(/s+/g, ""); 
  13.         case 2: 
  14.             return str.replace(/(^s*)|(s*$)/g, ""); 
  15.         case 3: 
  16.             return str.replace(/(^s*)/g, ""); 
  17.         case 4: 
  18.             return str.replace(/(s*$)/g, ""); 
  19.         default: 
  20.             return str; 
  21.     } 
  22.  
  23. /** 
  24.  * @param  {str}  
  25.  * @param  {type} 
  26.  *       type:  1:首字母大写  2:首页母小写  3:大小写转换  4:全部大写  5:全部小写 
  27.  * @return {String} 
  28.  */ 
  29. changeCase (str, type) { 
  30.     type = type || 4 
  31.     switch (type) { 
  32.         case 1: 
  33.             return str.replace(/bw+b/g, function (word) { 
  34.                 return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase(); 
  35.  
  36.             }); 
  37.         case 2: 
  38.             return str.replace(/bw+b/g, function (word) { 
  39.                 return word.substring(0, 1).toLowerCase() + word.substring(1).toUpperCase(); 
  40.             }); 
  41.         case 3: 
  42.             return str.split('').map( function(word){ 
  43.                 if (/[a-z]/.test(word)) { 
  44.                     return word.toUpperCase(); 
  45.                 }else{ 
  46.                     return word.toLowerCase() 
  47.                 } 
  48.             }).join('') 
  49.         case 4: 
  50.             return str.toUpperCase(); 
  51.         case 5: 
  52.             return str.toLowerCase(); 
  53.         default: 
  54.             return str; 
  55.     } 
  56.  
  57. /* 
  58.     检测密码强度 
  59. */ 
  60. checkPwd (str) { 
  61.     var Lv = 0; 
  62.     if (str.length < 6) { 
  63.         return Lv 
  64.     } 
  65.     if (/[0-9]/.test(str)) { 
  66.         Lv++ 
  67.     } 
  68.     if (/[a-z]/.test(str)) { 
  69.         Lv++ 
  70.     } 
  71.     if (/[A-Z]/.test(str)) { 
  72.         Lv++ 
  73.     } 
  74.     if (/[.|-|_]/.test(str)) { 
  75.         Lv++ 
  76.     } 
  77.     return Lv; 
  78.  
  79. /*过滤html代码(把<>转换)*/ 
  80. filterTag (str) { 
  81.     str = str.replace(/&/ig, "&amp;"); 
  82.     str = str.replace(/</ig, "&lt;"); 
  83.     str = str.replace(/>/ig, "&gt;"); 
  84.     str = str.replace(" ", "&nbsp;"); 
  85.     return str; 
  86. }  

(编辑:葫芦岛站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!