欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS實(shí)現(xiàn)手機(jī)號(hào)脫敏的方法詳解

 更新時(shí)間:2025年03月11日 11:18:09   作者:嚶嚶怪呆呆狗  
脫敏指的是通過(guò)特定的技術(shù)手段對(duì)敏感數(shù)據(jù)進(jìn)行處理,使其不再直接暴露給用戶或系統(tǒng),防止敏感信息泄露,通常在測(cè)試、開(kāi)發(fā)、數(shù)據(jù)處理等場(chǎng)景中使用,本文給大家介紹了JS實(shí)現(xiàn)手機(jī)號(hào)脫敏的方法,需要的朋友可以參考下

1、脫敏的含義

脫敏(Data Masking)指的是通過(guò)特定的技術(shù)手段對(duì)敏感數(shù)據(jù)進(jìn)行處理,使其不再直接暴露給用戶或系統(tǒng),防止敏感信息泄露,通常在測(cè)試、開(kāi)發(fā)、數(shù)據(jù)處理等場(chǎng)景中使用。脫敏后的數(shù)據(jù)應(yīng)該保留其格式和特征,但不應(yīng)包含敏感信息。

常見(jiàn)的脫敏方式

  • 字符替換:將敏感信息的一部分替換為特殊字符(如 *、X)。
  • 數(shù)據(jù)加密:通過(guò)加密算法將敏感數(shù)據(jù)加密后存儲(chǔ)或傳輸。
  • 數(shù)據(jù)脫標(biāo):刪除或用替代值替換敏感數(shù)據(jù)。
  • 局部脫敏:僅對(duì)敏感數(shù)據(jù)的部分進(jìn)行替換,而保留其他部分。

就是下面這種效果,這個(gè)在現(xiàn)在的生活中也是很常見(jiàn)的東西了

在這里插入圖片描述

2、前端處理手機(jī)號(hào)脫敏的方式

2.1 字符串的replace搭配正則

在這里插入圖片描述

核心點(diǎn)

  • String.prototype.replace()
  • 正則表達(dá)式
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <input type="text" id="phone" value="13012345678">
  <button id="btn">點(diǎn)擊</button>
  <br>
  脫敏后數(shù)據(jù):<span id="result"></span>
  <script>
    document.querySelector('#btn').onclick = function () {
      let phone = document.querySelector('#phone').value
      let newPhone = phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
      document.querySelector('#result').innerHTML = newPhone
    }
  </script>

</body>

</html>

在此處, (\d{3})\d{4}(\d{4}) 就是核心。這里的 $1 ,$2 只會(huì)匹配上以 () 包裹的東西 ,所以 $2 就是5678 , 不是1234。$1 指的就是第一個(gè)匹配上的大括號(hào),$2 指的就是 第二個(gè)匹配上的大括號(hào)

    $1 ==> (\d{ 3}) ==> 130
    $2 ==> (\d{4}) ==> 5678

2.2 字符串的slice

核心點(diǎn)

核心點(diǎn)就是利用了 字符串截取的方法

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <input type="text" id="phone" value="13012345678">
  <button id="btn">點(diǎn)擊</button>
  <br>
  脫敏后數(shù)據(jù):<span id="result"></span>
  <script>
    document.querySelector('#btn').onclick = function () {
      let phone = document.querySelector('#phone').value
      // 2、第二種方法 slice
      // slice(0,3) 截取前3位
      // slice(-4) 11+(-4) = 7 ,從第八個(gè)字符截取,一直到最后
      let newPhone = phone.slice(0, 3) + '****' + phone.slice(-4)
      document.querySelector('#result').innerHTML = newPhone
    }

    // $1 ==> (\d{ 3}) ==> 130
    // $2 ==> (\d{4}) ==> 5678
  </script>

</body>

</html>

2.3 數(shù)組的splice

核心點(diǎn)

  • 先轉(zhuǎn)成數(shù)組
  • 利用數(shù)組的 splice 方法,先刪除四個(gè),然后再插入 四個(gè)星號(hào)
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <input type="text" id="phone" value="13012345678">
  <button id="btn">點(diǎn)擊</button>
  <br>
  脫敏后數(shù)據(jù):<span id="result"></span>
  <script>
    document.querySelector('#btn').onclick = function () {
      // 3、數(shù)組拼接方法
      let phone = document.querySelector('#phone').value
      let arr = phone.split('')
      arr.splice(3, 4, '****')
      let newPhone = arr.join('')
      document.querySelector('#result').innerHTML = newPhone
    }

    // $1 ==> (\d{ 3}) ==> 130
    // $2 ==> (\d{4}) ==> 5678
  </script>

</body>

</html>

其實(shí)還有其他方法在此不列舉了,但還是第一種, 是最常見(jiàn)的方式

3、replace的特殊特?fù)Q模式

在這里插入圖片描述

    const str = "Hello dgg and world!";
    const regex = /(dgg)/;
    const result = str.match(regex);
    console.log(result);

    if (result) {
      const beforeMatch = str.slice(0, result.index);  // 匹配前的文本
      const afterMatch = str.slice(result.index + result[0].length);  // 匹配后的文本

      console.log("匹配前的文本:", beforeMatch);  // Hello,
      console.log("匹配后的文本:", afterMatch); // !
      console.log("匹配到的文本:", result[0]);   // Hello, world!
    }

在這里插入圖片描述

這個(gè)result 返回的是一個(gè)數(shù)組 ,

  • 數(shù)組第一項(xiàng):這個(gè)正則匹配到的整個(gè)字符串
  • 數(shù)組第二項(xiàng):第一個(gè)捕獲的組
  • 數(shù)組第三項(xiàng):第二個(gè)捕獲的組
  • 以此類推
  • index:匹配開(kāi)始的位置
  • input 原始輸入字符串

到此這篇關(guān)于JS實(shí)現(xiàn)手機(jī)號(hào)脫敏的方法詳解的文章就介紹到這了,更多相關(guān)JS手機(jī)號(hào)脫敏內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論