[英]Masking middle 4 digit number using Javascript
我有 Json 數組數據,如下所示:
{
"AccountNumber":[
"123456789012",
"234567891012",
"345678912012"
]
}
現在我只想屏蔽中間的 4 位數字。 如下所示:
{
"AccountNumber":[
"1234xxxx9012",
"2345xxxx1012",
"3456xxxx2012"
]
}
任何人都可以幫助如何使用 javascript 屏蔽多個 json 數組數據。
既然你說你使用的是 JSON,那么答案如下:
var json = '{"AccountNumber":["123456789012","234567891012","345678912012"]}'; var result = json.replace(/(\\d{4})(\\d{4})(\\d+)/g, '$1xxxx$3'); // this will be the JSON with the masked values console.log(result); // now you can parse the JSON and get some useful result console.log(JSON.parse(result));
但是,處理從 JSON 解析的數據可能更有用
var json = '{"AccountNumber":["123456789012","234567891012","345678912012"]}'; var data = JSON.parse(json); data.AccountNumber.forEach((v, i, a) => a[i] = `${v.substr(0, 4)}xxxx${v.substr(8)}` ); console.log(data);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.