- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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 some preceding characters with a constant 4 asterisks and display the last 3 as well - JavaScript?
Let’s say the following are our values −
'6778922' '76633 56 1443' '8888 4532 3232 9999'
We want the preceding characters to be replaced with 4 asterisks and the display rest of the last 3 characters. The output should be −
**** 922 **** 443 **** 999
For such conditions, use the replace() and set regex in it.
Example
Following is the code −
const hideDataWithDot = value => value.replace(/.+(.{3})$/, "**** $1"); console.log(hideDataWithDot('6778922')) console.log(hideDataWithDot('76633 56 1443')) console.log(hideDataWithDot('8888 4532 3232 9999'))
To run the above program, use the following command −
node fileName.js.
Here, my file name is demo236.js.
Output
The output is as follows −
PS C:\Users\Amit\javascript-code> node demo236.js **** 922 **** 443 **** 999
Advertisements
To Continue Learning Please Login
Login with Google