How to replace some preceding characters with a constant 4 asterisks and display the last 3 as well - JavaScript?
Question
How to replace some preceding characters with a constant 4 asterisks and display the last 3 as well - JavaScript?
Let’s say the following are our values −
'6778922' '76633 56 1443' '8888 4532 3232 9999'
We want the preceding characters to be replaced with 4 asterisks and the display rest of the last 3 characters. The output should be −
**** 922 **** 443 **** 999
For such conditions, use the replace() and set regex in it.
Example
Following is the code −
const hideDataWithDot = value => value.replace(/.+(.{3})$/, "**** $1"); console.log(hideDataWithDot('6778922')) console.log(hideDataWithDot('76633 56 1443')) console.log(hideDataWithDot('8888 4532 3232 9999'))
To run the above program, use the following command −
node fileName.js.
Here, my file name is demo236.js.
Output
The output is as follows −
PS C:\Users\Amit\javascript-code> node demo236.js **** 922 **** 443 **** 999
Advertisements
Not affiliated with Tutorialspoint