1、字符串每隔几位添加符号可用来格式化手机号、银行卡号、身份证号等
const str = '100000000'
str.replace(/(?=\B(\d{3})+$)/g, ',')
// '100,000,000'
2、格式化金额
function formatPrice(price, suffix = '') {
return price ? Number(price).toFixed(2) + suffix : 0
}
console.log(formatPrice(100),'元')
// 100.00 元
3、隐藏手机号中间四位str的类型需要为字符串
function formatPhone(str) {
return str? str.replace(/(\d{3})\d*(\d{4})/g, '$1***$2') : ''
}
console.log(formatPhone('13000000000'))
// 130***0000
4、隐藏身份证号中间几位str的类型需要为字符串
function formatIdentity(str) {
return str? str.replace(/(\d{3})\d*(\d{4})/g, '$1***********$2') : '';
}
console.log(formatIdentity('430726111122225412'))
// 430***********5412