Example: Let’s take a number 12345678 and the masking character is * and the number of the last element that we have to skip is 2 In the below image shows that we skip the last two-element, and we mask another character with ‘*’.
Approach: We will make one function that can mask the character of the string except for the last element we will give our number as a string.
- In this function we take three-argument, first one is a string which we want to mask, the second argument will specify mask character, and third is number of character that you want to skip
- Then we use replace function to replace the character with a specified mask. Replace all characters we use regex /./g where ‘.’ will match any character and g is tens for globally.
- Now we have to add the remaining character into the masking string, so we again use the slice function to get the remaining n character and add it into the masking string.