JS隐藏点好号码中间4位
function resetPhone(phone) { var str = String(phone) var len = str.length; var prev,next; if (len >= 7) { prev = str.slice(-len,-7) next = str.slice(-3) str = prev+"****"+next } else if (len < 7 && len >= 6) { prev = str.slice(-len,-4) next = str.slice(-2) str = prev + "**" + next } console.log(str) return str }
上面可以正常使用,正则更加方便,但是如果中间连续重复数字有点小问题(如下面的正则方式)。
function resetPhone(phone) { var str = String(phone) var len = str.length; if (len >= 7) { var reg = str.slice(-7, -3) return str.replace(reg, "****") } else if (len < 7 && len >= 6) { // var reg = str.slice(-4, -2) return str.replace(reg, "**") } }
相关推荐
阿斌Elements 2020-06-11
zrtlin 2020-11-09
xuebingnan 2020-11-05
wikiwater 2020-10-27
heheeheh 2020-10-19
Crazyshark 2020-09-15
softwear 2020-08-21
ZGCdemo 2020-08-16
jczwilliam 2020-08-16
littleFatty 2020-08-16
idning 2020-08-03
jinxiutong 2020-07-26
lanzhusiyu 2020-07-19
Skyline 2020-07-04
xiaofanguan 2020-06-25
Aveiox 2020-06-23