SoFunction
Updated on 2025-03-03

Regular expressions realize the 4-digit number in the middle of the mobile phone number or only the last four-digit number

Let's take a look at the regular expression to hide the 4-digit number in the middle of the mobile phone number or only display the last four-digit number.

// Match the beginning and end of the mobile phone number and output it in a form similar to "123****8901"'12345678901'.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');

This paragraph regularly matches the 11 consecutive digits in the string, replaces the 4 middle digits as *, and outputs the common format of hidden mobile phone numbers.

If you want to get only the last 4 digits, you can change it to the following form:

// Match 11 consecutive digits and replace the first 7 digits as * sign'15110280327'.replace(/\d{7}(\d{4})/, '*******$1');

ps: Let's take a look at the four digits in the middle of the hidden mobile phone number

1. Hide the four digits in the middle of the mobile phone number and become 186****9877

/**
  * Hide some mobile phone numbers
  * @param phone
  * @return
  */
 public static String hidePhoneNum(String phone){
 String result = "";
 if (phone != null && !"".equals(phone)) {
  if (isMobileNum(phone)) {
  result = (0, 3) + "****" + (7);
  }
 }
 return result;
 }

2. Determine whether it is a mobile phone number

/**
    * Check if it is a phone number
    *
    * @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 = (mobiles);
    return ();
  }

Summarize

The above is the regular expression introduced by the editor to you to hide the 4-digit number in the middle of the mobile phone number or only display the last four-digit number. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!