// 案例
{{ house || '' + address || '' }}
{{ house || '' }} {{ address || '' }}
{{ (house || '').concat(address || '') }}
{{ idCard && idCard.replace(/(.{6}).*(.{4})/, '$1********$2') }}
{{ idCard.replace(/^(\d{6})\d{8}(.{4}$)/g, `$1${Array(9).join('*')}$2`) }}
{{ idCard.replace(/(.{6}).*(.{4})/, '$1********$2') }}
{{ tel.replace(/^(\d{3})\d+(\d{4})$/, '$1****$2') }}
{{ name.replace(/(?<=.)./g, '*') }}
{{ name.replace(/.(?=.)/g, '*') }}
{{ money.replace(/(\d)(?=(\d{3})+$)/g, '$1,') }}
1. 身份证隐藏中间8位(出生年月日)
idCard.replace(/(.{6}).*(.{4})/, '$1********$2')
idCard.replace(/^(\d{6})\d{8}(.{4}$)/g, `$1${Array(9).join('*')}$2`)
// 强制只能输入身份证类型
numChange(e) {
if (e.length === 18) {
this.registerData.idNumber = e.replace(/[\W]/g, "");
} else {
this.registerData.idNumber = e.replace(/[^0-9]/gi, "");
}
},
2. 电话号码隐藏中间四位
tel.replace(/^(\d{3})\d+(\d{4})$/, '$1****$2')
3. 姓名只显示姓
name.replace(/(?<=.)./g, '*')
4. 姓名只显示最后一个字
name.replace(/.(?=.)/g, '*')
5. 转换成千分位
money.replace(/(\d)(?=(\d{3})+$)/g, '$1,')
6.输入的数字不能超过10万
let reg = /^([0-9]{1,5})(\.[0-9]{1,3}){0,1}$/
if (!reg.test(num)) {
return alert('输入的数字不能超过10万')
}
7.验证统一社会信用代码
let reg = /[^_IOZSVa-z\W]{2}\d{6}[^_IOZSVa-z\W]{10}$/g
8.验证最多可保留两位小数的正则:
var reg = /^\d+(.\d{1,2})?$/
tips:
在vue模板中支持语法:
vue模板中支持的拼接
{{ house || '' + address || '' }}{{ house || '' }} {{ address || '' }}{{ (house || '').concat(address || '') }}
根据身份证号计算年龄
computed: {
userAge() {
if (this.data.householdIdNumber != null) {
var yearBirth = this.data.householdIdNumber.substring(6,10);
var monthBirth = this.data.householdIdNumber.substring(10,12);
var dayBirth = this.data.householdIdNumber.substring(12,14);
var myDate = new Date();
var monthNow = myDate.getMonth() + 1;
var dayNow = myDate.getDate();
this.age = myDate.getFullYear() - yearBirth;
if(monthNow < monthBirth || (monthNow == monthBirth && dayNow < dayBirth)){
this.age--;
}
//返回年龄
return this.age;
} else {
return ''
}
}
},