JS實現(xiàn)手機號脫敏的方法詳解
1、脫敏的含義
脫敏(Data Masking)指的是通過特定的技術手段對敏感數(shù)據進行處理,使其不再直接暴露給用戶或系統(tǒng),防止敏感信息泄露,通常在測試、開發(fā)、數(shù)據處理等場景中使用。脫敏后的數(shù)據應該保留其格式和特征,但不應包含敏感信息。
常見的脫敏方式
- 字符替換:將敏感信息的一部分替換為特殊字符(如
*、X)。 - 數(shù)據加密:通過
加密算法將敏感數(shù)據加密后存儲或傳輸。 - 數(shù)據脫標:刪除或用替代值替換敏感數(shù)據。
- 局部脫敏:僅對
敏感數(shù)據的部分進行替換,而保留其他部分。
就是下面這種效果,這個在現(xiàn)在的生活中也是很常見的東西了

2、前端處理手機號脫敏的方式
2.1 字符串的replace搭配正則

核心點
- String.prototype.replace()
- 正則表達式
<!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">點擊</button>
<br>
脫敏后數(shù)據:<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 只會匹配上以 () 包裹的東西 ,所以 $2 就是5678 , 不是1234。$1 指的就是第一個匹配上的大括號,$2 指的就是 第二個匹配上的大括號
$1 ==> (\d{ 3}) ==> 130
$2 ==> (\d{4}) ==> 5678
2.2 字符串的slice
核心點
核心點就是利用了 字符串截取的方法
<!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">點擊</button>
<br>
脫敏后數(shù)據:<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 ,從第八個字符截取,一直到最后
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
核心點
- 先轉成數(shù)組
- 利用數(shù)組的 splice 方法,先刪除四個,然后再插入 四個星號
<!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">點擊</button>
<br>
脫敏后數(shù)據:<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>
其實還有其他方法在此不列舉了,但還是第一種, 是最常見的方式
3、replace的特殊特換模式

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!
}

這個result 返回的是一個數(shù)組 ,
- 數(shù)組第一項:這個正則匹配到的整個字符串
- 數(shù)組第二項:第一個捕獲的組
- 數(shù)組第三項:第二個捕獲的組
- 以此類推
- index:匹配開始的位置
- input 原始輸入字符串
到此這篇關于JS實現(xiàn)手機號脫敏的方法詳解的文章就介紹到這了,更多相關JS手機號脫敏內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
JavaScript實現(xiàn)鼠標經過表格行給出顏色標識
這篇文章主要為大家詳細介紹了JavaScript實現(xiàn)鼠標經過表格行給出顏色標識,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-04-04

