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

JavaScript常用工具方法封装

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

(编辑:文山站长网)

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

热点阅读