JavaScript - How to Mask All Characters Except the Last? Last Updated : 07 Dec, 2024 Comments Improve Suggest changes Like Article Like Report Here are the different methods to replace characters except the last with the specified mask character in JavaScript.1. Using slice() and repeat() MethodsUse slice() to get the last character of the string and repeat() to mask the rest of the characters. JavaScript let s = "123456789"; let maskChar = "*"; let masked = maskChar.repeat(s.length - 1) + s.slice(-1); console.log(masked); Output********9 maskChar.repeat(s.length - 1) creates a string of * characters for all but the last character.s.slice(-1) gets the last character of the string.The result is a concatenation of the masked part and the last character.2. Using substring() and repeat() MethodsThis approach is quite similar to the first, but instead of slice(), we use substring() to get the last character. JavaScript let s = "9876543210"; let maskChar = "#"; let masked = maskChar.repeat(s.length - 1) + s.substring(s.length - 1); console.log(masked); Output#########0 maskChar.repeat(s.length - 1) repeats the mask character for all but the last character.s.substring(s.length - 1) extracts the last character.3. Using Array.map() and join()In this we convert the string into an array, masking all but the last character, and then using join() to form the final string. JavaScript let s = "abcdefgh"; let maskChar = "$"; let masked = s.split('').map((char, idx) => idx === s.length - 1 ? char : maskChar).join(''); console.log(masked); Output$$$$$$$h s.split('') converts the string into an array of characters.map() is used to replace each character with the mask unless it's the last character.join('') reassembles the array back into a string.4. Using replace() with a Regular ExpressionThis approach uses a regular expression to match all characters except the last one and replace them with the mask character. JavaScript let s = "abcdefg"; let maskChar = "&"; let masked = s.replace(/.(?=.{1})/g, maskChar); console.log(masked); Output&&&&&&g The regex /(.?=.{1})/g matches all characters except the last one.replace() replaces the matched characters with the mask character (&).5. Using for Loop with String ConcatenationThis is a more manual approach where a loop is used to iterate over the string and build the masked string. JavaScript let s = "987654321"; let maskChar = "+"; let masked = ""; for (let i = 0; i < s.length - 1; i++) { masked += maskChar; } masked += s[s.length - 1]; console.log(masked); Output++++++++1 The loop iterates through the string and adds the mask character for each character except the last one.The last character is appended directly after the loop.6. Using StringBuilder Approach (Manual String Concatenation)For larger strings or performance-sensitive situations, manually building a string using string concatenation can be a choice. JavaScript let s = "hello123"; let maskChar = "X"; let masked = ""; for (let i = 0; i < s.length - 1; i++) { masked += maskChar; } masked += s.charAt(s.length - 1); console.log(masked); OutputXXXXXXX3 Concatenate X for all characters except the last one using a loop.charAt(s.length - 1) retrieves the last character. Comment More infoAdvertise with us Next Article JavaScript - How to Mask All Characters Except the Last? khushindpatel Follow Improve Article Tags : JavaScript Web Technologies javascript-string javascript-functions JavaScript-DSA JavaScript-RegExp JavaScript-Questions +3 More Similar Reads JavaScript - How To Get The Last Caracter of a String? Here are the various approaches to get the last character of a String using JavaScript.1. Using charAt() Method (Most Common)The charAt() method retrieves the character at a specified index in a string. To get the last character, you pass the index str.length - 1.JavaScriptconst s = "JavaScript"; co 3 min read JavaScript - How to Get the First Three Characters of a String? Here are the various methods to get the first three characters of a string in JavcaScript1. Using String.slice() MethodThe slice() method is one of the most commonly used and versatile methods to extract a part of a string. It allows you to specify the start and end positions for slicing the string. 3 min read JavaScript - How to Find the First and Last Character of a String? Here are the various methods to find the first and last character of a string in JavaScript.1. Using charAt() MethodThe charAt() method retrieves the character at a specified index.JavaScriptconst s = "JavaScript"; const first = s.charAt(0); const last = s.charAt(s.length - 1); console.log(first); c 2 min read JavaScript - Add a Character to the End of a String These are the following ways to insert a character at the end of the given string:1. Using ConcatenationWe can use the + operator or template literals to append the character.JavaScriptlet str = "Hello GFG"; let ch = "!"; let res = str + ch; console.log(res); OutputHello GFG! 2. Using Template Liter 2 min read How to get the last character of a string in PHP ? In this article, we will find the last character of a string in PHP. The last character can be found using the following methods.Using array() Method: In this method, we will find the length of the string, then print the value of (length-1). For example, if the string is "Akshit" Its length is 6, in 2 min read How to remove all Non-ASCII characters from the string using JavaScript ? In this article, we are given a string containing some non-ASCII characters and the task is to remove all non-ASCII characters from the given string. Approaches to remove all Non-ASCII Characters from String: Table of Content Using ASCII values in JavaScript regExUsing Unicode in JavaScript regExUsi 3 min read JavaScript - Delete Character from JS String In JavaScript, characters can be deleted from the beginning, end, or any specific position in a string. JavaScript provides several methods to perform these operations efficiently.Delete First CharacterTo remove the first character from a string, we can use methods like slice, substring, or regular 2 min read How to Get Character of Specific Position using JavaScript ? Get the Character of a Specific Position Using JavaScript We have different approaches, In this article we are going to learn how to Get the Character of a Specific Position using JavaScript Below are the methods to get the character at a specific position using JavaScript: Table of Content Method 1 4 min read JavaScript - How to Pad a String to Get the Determined Length? Here are different ways to pad a stirng to get the specified length in JavaScript.1. Using padStart() MethodThe padStart() method can be used to pad a string with the specified characters to the specified length. It takes two parameters, the target length, and the string to be replaced with. If a nu 3 min read JavaScript - Delete First Character of a String To delete the first character of a string in JavaScript, you can use several methods. Here are some of the most common onesUsing slice()The slice() method is frequently used to remove the first character by returning a new string from index 1 to the end.JavaScriptlet s1 = "GeeksforGeeks"; let s2 = s 1 min read Like