正則表達式實現手機號中間4位數隱藏或者只顯示末尾四位數


下面看下正則表達式實現手機號中間4位數隱藏或者只顯示末尾四位數

?
1
2
// 匹配手機號首尾,以類似“123****8901”的形式輸出
'12345678901' .replace(/(\d{3})\d{4}(\d{4})/, '$1****$2' );

此段正則匹配字符串中的連續11位數字,替換中間4位為*號,jQuery特效輸出常見的隱匿手機號的格式。

如果要僅得到末尾4位,則可以改成如下形式:

?
1
2
// 匹配連續11位數字,並替換其中的前7位為*號
'15110280327' .replace(/\d{7}(\d{4})/, '*******$1' );

ps:下面看下隱藏手機號碼中間四位數

1.隱藏手機號碼中間四位,變成186****9877

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
  * 隱藏部分手機號碼
  * @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.判斷是否是手機號碼

?
1
2
3
4
5
6
7
8
9
10
11
/**
    * 檢查是否是電話號碼
    *
    * @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();
   }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM