java
public class Test {
public static void main(String[] args) {
String phone = "18771638976";
System.out.println(phone.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2")); //11位 187****8976
String idCard = "410721992081659807";
System.out.println(idCard.replaceAll("(\\d{4})\\d{10}(\\d{4})","$1****$2")); //18位 4107**********9807 只适用于纯数字不带字母
}
}
javaScript
phone.substr(0, 3) + '****' + phone.substr(7);
phone=phone.replace(/(\w{3})\w*(\w{4})/, '$1****$2');
cardNo = cardNo.substring(0, 6) + "********" + cardNo.substring(14, 18);
cardNo=cardNo.replace(/(\w{6})\w*(\w{4})/, '$1******$2'); 适用于纯数字,适用于数字+字母