//隐藏名字 formatName = (name)=>{ let newStr = ''; if(name.length === 2){ newStr = name.substr(0,1)+'*'; }else if(name.length>2){ let char = ''; for(let i=0,len = name.length-2;i<len;i++){ char += '*'; } newStr = name.substr(0,1)+char+name.substr(-1,1); }else{ newStr = name } return newStr; } //隐藏电话 formatTel = (tel)=>{ tel = "" + tel; let newTel = tel.substr(0,3) + "****" + tel.substr(7) return newTel; } formatAddressX=(addr)=>{ var nums = String(addr.match(/d+(.d+)?(%)?(‰)?/g)); var numArray = nums.split(','); var compare = function (x, y) { if (parseFloat(x) < parseFloat(y)) { return 1; } else if (parseFloat(x) > parseFloat(y)) { return -1; } else { return 0; } } //按数组的大小降序排列,很大程度上减少执行以下全局replace而带来的匹配不了问题 numArray.sort(compare) for(var i=0; i<numArray.length;i++){ var pattern = new RegExp(numArray[i],"gm"); addr = addr.replace(pattern,"*"); } return addr; }