简体   繁体   中英

Masking middle 4 digit number using Javascript

I have Json Array data as shown below:

{
    "AccountNumber":[
        "123456789012",
        "234567891012",
        "345678912012"
    ]
}

Now I want to mask the middle 4 digit number only. as shown below:

{
    "AccountNumber":[
        "1234xxxx9012",
        "2345xxxx1012",
        "3456xxxx2012"
    ]
}

Can anyone help how to mask multiple json array data using javascript.

Since you say your working with JSON, the answer is as follows:

 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));

However, it may be more useful to work with data parsed from 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM