Skip to content
Related Articles

Related Articles

How to replace characters except last with the specified mask character in JavaScript ?

View Discussion
Improve Article
Save Article
  • Last Updated : 27 Jul, 2022

In this article, we have given a number and the task is to replace the characters except for the last character with a specified mask character in JavaScript.

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
  • We use some inbuilt JavaScript function slice to slice our given string here we write slice(0,-n) where -n will skip the last n character of the string.
  • 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.

Example:

Javascript




<script>
    // Function for masking the character
    function MaskCharacter(str, mask, n = 1) {
 
        // Slice the string and replace with
        // mask then add remaining string
        return ('' + str).slice(0, -n)
            .replace(/./g, mask)
            + ('' + str).slice(-n);
    }
 
    // Number that we want to mask
    var num = 12345678;
 
    // Convert number into string
    var str = num.toString();
 
    // Call function without giving value of n
    console.log(MaskCharacter(str, '#'));
 
    // Call function with value of n
    console.log(MaskCharacter(str, '#', 2));
</script>

Output:

Approach#2 : This task can also be perform with the help of reduce method. 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.
  • Spread string into array of character and use reduce function on array which check that index of element is less than number of character we want skip or not.
  • If index is less than the number then add mask to result else add character to result and at last return ans. 

Example:

Javascript




<script>
     
    // Function for masking the character
    function MaskCharacter(str, mask, n = 1) {
         
        // Slice the string and replace with
        // mask then add remaining string
        return [...str].reduce((acc, x, i) =>
                    (i < str.length - n) ? acc+mask : acc+x , '');
    }
 
    // Number that we want to mask
    var num = 12345678;
 
    // Convert number into string
    var str = num.toString();
 
    // Call function without giving value of n
    console.log(MaskCharacter(str, '#'));
 
    // Call function with value of n
    console.log(MaskCharacter(str, '#', 2));
 
</script>

Output:

reduce() function


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!