当前位置:网站首页>常用超好用正则表达式!
常用超好用正则表达式!
2022-07-26 16:43:00 【程序猿叔叔】
常用超好用正则表达式!
数据过滤
- 手机号码隐藏中间4位
- 保留两位小数
- 每三位逗号隔开,后面补两位小数,多用于金额数字
- 银行尾号四位数
- 数字金额转换为大写人民币汉字的方法
- 获取Url携带的地址参数
- 金额以元和万元为单位
- 隐藏证件号
- 用户姓名脱敏
- 账号4位一空格
- 字符全局替换
数据验证
- 手机号验证
- 姓名校验
- 密码必须为8-18位数,且包含大小写字母和特殊符号
- 身份证号完整校验
- 只能输入数字和字母
- 校验特殊字符
数据过滤
手机号码隐藏中间4位
phoneHideMiddle(val) {
if (val) {
return `${
val.substring(0, 3)}****${
val.substring(val.length - 4)}`
}
else {
return "";
}
}
保留两位小数
keepTwoNum(val) {
val = Number(val);
return val.toFixed(2);
}
每三位逗号隔开,后面补两位小数,多用于金额数字
floatThree(value) {
// console.log(value)
value = "" + value;
if (!value) return '0.00';
// var intPart = Number(value).toFixed(0); //获取整数部分
var intPart = parseInt(Number(value));//获取整数部分
// console.log('intPart',intPart)
var intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{
3})+$)/g, '$1,'); //将整数部分逢三一断
// console.log('intPartFormat',intPartFormat)
var floatPart = ".00"; //预定义小数部分
var value2Array = value.split(".");
//=2表示数据有小数位
if (value2Array.length == 2) {
floatPart = value2Array[1].toString(); //拿到小数部分
// console.log('floatPart',floatPart)
if (floatPart.length == 1) {
//补0,实际上用不着
return intPartFormat + "." + floatPart + '0';
} else {
return intPartFormat + "." + floatPart;
}
} else {
return intPartFormat + floatPart;
}
},
银行尾号四位数
bankCardNumLastFour(val) {
// val = Number(val);
if (val) {
return val.substring(val.length - 4);
}
},
数字金额转换为大写人民币汉字的方法
convertCurrency(money) {
//汉字的数字
var cnNums = new Array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
//基本单位
var cnIntRadice = new Array('', '拾', '佰', '仟');
//对应整数部分扩展单位
var cnIntUnits = new Array('', '万', '亿', '兆');
//对应小数部分单位
var cnDecUnits = new Array('角', '分', '毫', '厘');
//整数金额时后面跟的字符
var cnInteger = '整';
//整型完以后的单位
var cnIntLast = '元';
//最大处理的数字
var maxNum = 999999999999999.9999;
//金额整数部分
var integerNum;
//金额小数部分
var decimalNum;
//输出的中文金额字符串
var chineseStr = '';
//分离金额后用的数组,预定义
var parts;
if (money == '') {
return ''; }
money = parseFloat(money);
if (money >= maxNum) {
//超出最大处理数字
return '';
}
if (money == 0) {
chineseStr = cnNums[0] + cnIntLast + cnInteger;
return chineseStr;
}
//转换为字符串
money = money.toString();
if (money.indexOf('.') == -1) {
integerNum = money;
decimalNum = '';
} else {
parts = money.split('.');
integerNum = parts[0];
decimalNum = parts[1].substr(0, 4);
}
//获取整型部分转换
if (parseInt(integerNum, 10) > 0) {
var zeroCount = 0;
var IntLen = integerNum.length;
for (var i = 0; i < IntLen; i++) {
var n = integerNum.substr(i, 1);
var p = IntLen - i - 1;
var q = p / 4;
var m = p % 4;
if (n == '0') {
zeroCount++;
} else {
if (zeroCount > 0) {
chineseStr += cnNums[0];
}
//归零
zeroCount = 0;
chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
}
if (m == 0 && zeroCount < 4) {
chineseStr += cnIntUnits[q];
}
}
chineseStr += cnIntLast;
}
//小数部分
if (decimalNum != '') {
var decLen = decimalNum.length;
for (var i = 0; i < decLen; i++) {
var n = decimalNum.substr(i, 1);
if (n != '0') {
chineseStr += cnNums[Number(n)] + cnDecUnits[i];
}
}
}
if (chineseStr == '') {
chineseStr += cnNums[0] + cnIntLast + cnInteger;
} else if (decimalNum == '') {
chineseStr += cnInteger;
}
return chineseStr;
},
获取Url携带的地址参数
GetQueryString(name) {
//index.html?token=9b68dd98306327bf&action=2
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
if (window.location.search != "") {
var r = window.location.search.substr(1).match(reg);
} else {
if (window.location.hash.indexOf("?") > 0) {
var tmp = window.location.hash.split("?");
var r = tmp[1].match(reg);
} else {
var r = null;
}
}
if (r != null) {
return decodeURI(r[2]);
}
return null;
},
金额以元和万元为单位
moneyConvert(num) {
var newNum = Number(num);
if (10000 <= newNum) {
return (newNum / 10000) + '万元'
} else {
return newNum + '元'
}
},
隐藏证件号
certIdHide(val) {
if (val) {
var certId = val.substring(0, 1) + '****************' + val.substring(val.length - 1)
return certId
}
},
用户姓名脱敏
userNameHide(val) {
if (val) {
let name;
if (val.length < 3) {
name = '*' + val.substring(val.length - 1)
return name
} else {
name = val.substring(0, 1) + '*' + val.substring(val.length - 1)
return name
}
}
},
账号4位一空格
accountNumberSpace(val) {
if (val) {
return val.replace(/\s/g, "")
.replace(/\D/g, "")
.replace(/(\d{
4})(?=\d)/g, "$1 ");
}
}
字符全局替换
res.data.content = res.data.content.replace(/\<img/gi, '<img style="max-width:100%;height:auto" ')
数据验证
校验特殊字符
#### 校验特殊字符
isSpecialChars(str) {
var regEn = /[`~!@#$%^&*()_+<>?:"{
},.\/;'[\]]/im;
var regCn = /[·!#¥(——):;“”‘、,|《。》?、【】[\]]/im;
console.log(regEn.test(str))
if (regEn.test(str) || regCn.test(str)) {
return false
} else {
return true
}
}
手机号验证
#### 手机号验证
isvalidPhone(str) {
const reg = /^1\d{
10}$/
return reg.test(str)
},
姓名校验
#### 姓名校验
isUserName(name) {
if (name && name.length > 1) {
const reg = name.match(/^[\u4e00-\u9fa5]+$/)
return Boolean(reg)
}
return false
},
密码必须为8-18位数,且包含大小写字母和特殊符号
#### 密码必须为8-18位数,且包含大小写字母和特殊符号
/^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[!~@#$%^&*,\.])[0-9a-zA-Z!~@#$%^&*,\\.]{
8,18}$/
只能输入数字和字母
isvalidateLetterAndNum(str) {
const reg = /^[0-9a-zA-Z]+$/;
console.log(reg.test(str));
return reg.test(str)
},
校验特殊字符
isSpecialChars(str) {
var regEn = /[`~!@#$%^&*()_+<>?:"{
},.\/;'[\]]/im;
var regCn = /[·!#¥(——):;“”‘、,|《。》?、【】[\]]/im;
console.log(regEn.test(str))
if (regEn.test(str) || regCn.test(str)) {
return false
} else {
return true
}
}
边栏推荐
- 6种方法帮你搞定SimpleDateFormat类不是线程安全的问题
- Why are test / development programmers who are better paid than me? Abandoned by the times
- How to write plug-ins quickly with elisp
- 环境搭建-MongoDB
- 浅谈云原生边缘计算框架演进
- How does the data link layer transmit data
- API analysis of Taobao / tmall shipping address list and express delivery fees
- After Oracle creates a table partition, the partition is not given during the query, but the value specified for the partition field will be automatically queried according to the partition?
- Crazy God redis notes 02
- Comparison between dimensional modeling and paradigm modeling
猜你喜欢

The user experience center of Analysys Qianfan bank was established to help upgrade the user experience of the banking industry

Advantages of time series database and traditional database

Establishment of Eureka registration center Eureka server

2019 popularization group summary

Thoroughly uncover how epoll realizes IO multiplexing

Operating system migration practice: deploying MySQL database on openeuler
![[ctfshow-web]反序列化](/img/cd/b76e148adfc4d61049ab2cf429d4d7.png)
[ctfshow-web]反序列化

【OpenCV 例程 300篇】240. OpenCV 中的 Shi-Tomas 角点检测

Method and voltage setting of exciting vibrating wire sensor with hand-held vibrating wire collector
![37. [categories of overloaded operators]](/img/67/b821270079589c53b9c38b0ca033ac.png)
37. [categories of overloaded operators]
随机推荐
Create MySQL function: access denied; you need (at least one of) the SUPER privilege(s) for this operation
Everything is available Cassandra: the fairy database behind Huawei tag
How to write plug-ins quickly with elisp
Anaconda download and Spyder error reporting solution
Use dired to move files quickly
Tcpdump命令详解
Idea Alibaba cloud multi module deployment
Xiaomi Wuhan headquarters building starts today! Lei Jun: planned according to the scale of 10000 people
Concepts and differences of DQL, DML, DDL and DCL
maximum likelihood estimation
【开发教程7】疯壳·开源蓝牙心率防水运动手环-电容触摸
【开发教程9】疯壳·ARM功能手机-I2C教程
After Oracle creates a table partition, the partition is not given during the query, but the value specified for the partition field will be automatically queried according to the partition?
Application of machine vision in service robot
Win11 auto delete file setting method
Stop using xshell and try this more modern terminal connection tool
现在网上开户安全么?股票开户要找谁?
Merge multiple row headers based on apache.poi operation
The first self-developed embedded 40nm industrial scale memory chip in China was released, breaking the status quo that the localization rate is zero
[classification] vgg16 training record