- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to replace characters except last with a mask character in JavaScript?
JavaScript has a built-in function called replace() that can be used to replace characters in a string. This function takes two arguments: the first argument is the character or characters to be replaced, and the second argument is the character or characters to replace them with.
Syntax
Following is the syntax to replace characters except last with a specified mask −
str.replace(/.(?=.)/g, "x");
The first argument to the replace() method is character/s to be replaced. We pass a regular expression for all characters except last as the first argument to the replace method. All characters except the last are replaced with a specified mask ‘x’.
Algorithm
STEP 1 − Create a variable named str and assign the sting to it.
STEP 2 − Replace the characters using the String replace() method. Use the syntax discussed above.
STEP 3 − Display the string with replaced characters.
Example 1
In the program below, we replace all characters except the last one with a specified mask character ie. “x”. We use the above-defined syntax of the replace method.
<html> <body> <div id="result1"></div> <div id="result2"></div> <script> var str = "Hello world"; document.getElementById("result1").innerHTML = "Original String: " + str var newStr = str.replace(/.(?=.)/g, "x"); document.getElementById("result2").innerHTML = "String after replacement: " + newStr </script> </body> </html>
In the example above, the replace() function replaces all characters except the last one with the character "x".
If you want to replace only the first occurrence of a character, you can use the replace() function like this −
var str = "Hello world!"; var newStr = str.replace("l", "x", 1);
Example 2
In the example below, we replace all characters except last with a mask character. We take the string and mask character from the user.
<!doctype html> <html> <head> <title>Examples</title> </head> <body> <p>Enter String: </p> <input type="text" id="myStr" value=""> <p>Enter mask character: </p> <input type="text" id="myMask" value=""> <p> String after replacement:</p> <div id="result"></div> <button onclick="replaceStr()">Replace</button> <script> function replaceStr() { var str = document.getElementById("myStr").value; var mask = document.getElementById("myMask").value; var newStr = str.replace(/.(?=.)/g, mask); document.getElementById("result").innerHTML = newStr } </script> </body> </html>
There are many benefits of using the replace() function. First, it is very easy to use. Second, it is very versatile and can be used to replace characters in a string with any other character or characters.
In conclusion, the replace() function is a very useful function that can be used to replace characters in a string. It is very easy to use and is very versatile.
- Related Articles
- Python program to replace all Characters of a List except the given character
- How to replace all the special characters following another character – JavaScript?
- With JavaScript RegExp find a character except newline?
- Replace all characters in a string except the ones that exist in an array JavaScript
- How to replace some preceding characters with a constant 4 asterisks and display the last 3 as well - JavaScript?
- JavaScript regex - How to replace special characters?
- Program to replace all the characters in of a file with '#' except a particular word in Java
- MySQL query to get a substring from a string except the last three characters?
- String replace multiple characters with an asterisk in JavaScript
- MySQL query to remove everything except the last 7 characters in column record
- Python Pandas - Mask and replace NaNs with a specific value
- How to build a string with no repeating character n separate list of characters? in JavaScript
- How to Replace characters in a Golang string?
- How do I replace a character at a particular index in JavaScript?
- How to replace a character in a MySQL table?
