js 字符串类工具

	/**
			 * 去除空格
			 * @param  {str}
			 * @param  {type}
			 *       type:  1-所有空格  2-前后空格  3-前空格 4-后空格
			 * @return {String}
			 */
			trim(str, type) {
				type = type || 1
				switch (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;
				}
			}

			/**
			 * @param  {str}
			 * @param  {type}
			 *       type:  1:首字母大写  2:首页母小写  3:大小写转换  4:全部大写  5:全部小写
			 * @return {String}
			 */
			changeCase(str, type) {
				type = type || 4
				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 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;
				}
			}

			/*
			    检测密码强度
			*/
			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;
			}

			/*过滤html代码(把<>转换)*/
			filterTag(str) {
				str = str.replace(/&/ig, "&amp;");
				str = str.replace(/</ig, "&lt;");
				str = str.replace(/>/ig, "&gt;");
				str = str.replace(" ", " ");
				return str;
			}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xvzhengyang

感谢,励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值