下面看下正則表達式實現(xiàn)手機號中間4位數(shù)隱藏或者只顯示末尾四位數(shù)
// 匹配手機號首尾,以類似“123****8901”的形式輸出 '12345678901'.replace(/(d{3})d{4}(d{4})/, '$1****$2');
此段正則匹配字符串中的連續(xù)11位數(shù)字,替換中間4位為*號,輸出常見的隱匿手機號的格式。
如果要僅得到末尾4位,則可以改成如下形式:
// 匹配連續(xù)11位數(shù)字,并替換其中的前7位為*號 '15110280327'.replace(/d{7}(d{4})/, '*******$1');
ps:下面看下隱藏手機號碼中間四位數(shù)
1.隱藏手機號碼中間四位,變成186****9877
/** * 隱藏部分手機號碼 * @param phone * @return */ public static String hidePhoneNum(String phone){ String result = ""; if (phone != null && !"".equals(phone)) { if (isMobileNum(phone)) { result = phone.substring(0, 3) + "****" + phone.substring(7); } } return result; }
2.判斷是否是手機號碼
/** * 檢查是否是電話號碼 * * @return */ public static boolean isMobileNum(String mobiles) { Pattern p = Pattern .compile("^((13[0-9])|(14[0-9])|(15[^4,\D])|(18[0-9]))\d{8}$"); Matcher m = p.matcher(mobiles); return m.matches(); }
總結(jié)
以上所述是小編給大家介紹的正則表達式實現(xiàn)手機號中間4位數(shù)隱藏或者只顯示末尾四位數(shù),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對網(wǎng)站的支持!