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 - 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 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 JavaScript - Keep Only First N Characters in a String Here are the different methods to keep only the first N characters in a string1. Using slice() MethodThe slice() method is one of the most common and efficient ways to extract a substring from a string. We can easily keep only the first N characters by passing 0 as the start index and N as the end i 3 min read Remove a Character From String in JavaScript In JavaScript, a string is a group of characters. Strings are commonly used to store and manipulate text data in JavaScript programs, and removing certain characters is often needed for tasks like:Removing unwanted symbols or spaces.Keeping only the necessary characters.Formatting the text.Methods t 3 min read JavaScript - Delete Character at a Given Position in a String These are the following ways to Delete Character at a Given Position in a String:1. Using slice() MethodThe slice() method allows you to extract portions of a string. By slicing before and after the target position, you can omit the desired character.JavaScriptlet str = "Hello GFG"; let idx = 5; let 1 min read How to remove First and Last character from String in Scala? In this article, we will explore different approaches to removing the first and last character from a string in Scala. Table of Content Using substring methodUsing drop and dropRight methodsUsing substring methodIn this approach, we are using the substring method which takes two arguments the starti 1 min read How to Remove Last Character from String in Ruby? Removing the last character from a string in Ruby is a common task in various programming scenarios. Whether you need to manipulate user input, process file paths, or clean up data, there are several approaches to achieve this. This article focuses on discussing how to remove the last character from 2 min read Like