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

2、前端處理手機(jī)號脫敏的方式
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 只會匹配上以 () 包裹的東西 ,所以 $2 就是5678 , 不是1234。$1 指的就是第一個匹配上的大括號,$2 指的就是 第二個匹配上的大括號
$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 ,從第八個字符截取,一直到最后
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 方法,先刪除四個,然后再插入 四個星號
<!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í)還有其他方法在此不列舉了,但還是第一種, 是最常見的方式
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!
}

這個result 返回的是一個數(shù)組 ,
- 數(shù)組第一項(xiàng):這個正則匹配到的整個字符串
- 數(shù)組第二項(xiàng):第一個捕獲的組
- 數(shù)組第三項(xiàng):第二個捕獲的組
- 以此類推
- index:匹配開始的位置
- input 原始輸入字符串
到此這篇關(guān)于JS實(shí)現(xiàn)手機(jī)號脫敏的方法詳解的文章就介紹到這了,更多相關(guān)JS手機(jī)號脫敏內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript實(shí)現(xiàn)鼠標(biāo)經(jīng)過表格行給出顏色標(biāo)識
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)鼠標(biāo)經(jīng)過表格行給出顏色標(biāo)識,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-04-04
微信小程序?qū)崿F(xiàn)跳轉(zhuǎn)的幾種方式總結(jié)(推薦)
這篇文章主要介紹了微信小程序跳轉(zhuǎn)方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
JavaScript關(guān)于提高網(wǎng)站性能的幾點(diǎn)建議(一)
這篇文章主要介紹了JavaScript關(guān)于提高網(wǎng)站性能的幾點(diǎn)建議(一)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07
JavaScript中兩種鏈?zhǔn)秸{(diào)用實(shí)現(xiàn)代碼
方法鏈一般適合對一個對象進(jìn)行連續(xù)操作(集中在一句代碼)。一定程度上可以減少代碼量,缺點(diǎn)是它占用了函數(shù)的返回值。2011-01-01
jQuery實(shí)現(xiàn)騰訊信用界面(自制刻度尺)樣式
這篇文章主要介紹了jQuery實(shí)現(xiàn)騰訊信用界面(自制刻度尺)樣式,下文還總結(jié)了關(guān)于jquery中extend的方法,需要的朋友可以參考下2017-08-08

