SoFunction
Updated on 2025-04-11

Sample code for JavaScript to implement sensitive information desensitization

Desensitization of bank card number

To desensitize bank card information in JavaScript, you can use string processing to replace sensitive information with specific characters. Here is a simple example code to replace the middle number of the bank card number with "*":

function desensitizeCardNumber(cardNumber) {
  // Get the bank card number length  const length = ;

  // If the length of the bank card number is less than or equal to 4, there is no need to desensitize and return to the original bank card number directly  if (length <= 4) {
    return cardNumber;
  }

  // Take out the first four  const firstTwo = (0, 2);
  // Take out the last four  const lastFour = (length - 4);
  // Construct a desensitized string, and replace the number in the middle with "*"  const middle = "*".repeat(length - 6);

  // Combine into a desensitized bank card number  const desensitizedCardNumber = firstTwo + middle + lastFour;

  return desensitizedCardNumber;
}

// testconst cardNumber = "1234567890123456";
const desensitizedNumber = desensitizeCardNumber(cardNumber);
(desensitizedNumber); // Output: "12*********3456"

Name desensitization

To desensitize names in JavaScript, different desensitization strategies can be adopted according to actual needs. Here is a simple example code that replaces the last few characters of a name with "*":

function desensitizeName(name) {
  // Get the name length  const length = ;

  // If the name length is less than or equal to 1, there is no need to desensitize and return to the original name directly  if (length <= 1) {
    return name;
  }

  // Take out the first character  const firstChar = (0, 1);

  // Construct a desensitized string, except for the first character, all characters are replaced with "*"  const desensitizedPart = "*".repeat(length - 1);

  // Name after desensitization  const desensitizedName = firstChar + desensitizedPart;

  return desensitizedName;
}

// testconst name = "Zhang San";
const desensitizedName = desensitizeName(name);
(desensitizedName); // Output: "Zhang*"

Desensitization of mobile phone number

To desensitize mobile phone numbers in JavaScript, different desensitization strategies can be adopted according to actual needs. Here is a simple example code to replace the last four digits of the mobile phone number with "*":

function desensitizePhoneNumber(phoneNumber) {
  // Get the length of the mobile phone number  const length = ;

  // If the length of the mobile phone number is less than or equal to 7, there is no need to desensitize and return to the original mobile phone number directly  if (length <= 7) {
    return phoneNumber;
  }

  // Take out the first three  const firstThree = (0, 3);
  // Take out the last four  const lastFour = (length - 4);
  // Construct a desensitized string, and replace the middle four digits with "*"  const middle = "*".repeat(length - 7);

  // Combine into a desensitized mobile phone number  const desensitizedPhoneNumber = firstThree + middle + lastFour;

  return desensitizedPhoneNumber;
}

// testconst phoneNumber = "13812345678";
const desensitizedPhoneNumber = desensitizePhoneNumber(phoneNumber);
(desensitizedPhoneNumber); // Output: "138****5678"

This is the article about JavaScript's sample code to implement sensitive information desensitization. For more relevant JavaScript sensitive information desensitization content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!