1

I am trying to resetting password in react Js. I need to display that email is sent to their email Id. I don't want to display their whole email instead I want to replace some characters with *. For example, someone has an email testing@gmail.com so I want to replace some random character with * ie tes***g@gmail.com. How could I do that in javascript?? My code:

let a = testing@gmail.com
let c = a.split('@');
let b = c[0].slice(0,-3)+'***'+'@'+c[1];

but it is not efficient way.How to do efficiently??

suppose if i have test@gmail.com then it should display t**t@gmail.com and if any@gmail.com it should be like a*y@gmail.com.

  • *"but it is not efficient way."* Why not? – str Jul 09 '18 at 11:36
  • i want to slice from second last not from last one –  Jul 09 '18 at 11:37
  • [How to slice string from the end in JavaScript?](https://stackoverflow.com/questions/42308976/how-to-slice-string-from-the-end-in-javascript) – str Jul 09 '18 at 11:38
  • Try regex replace – Coli Jul 09 '18 at 11:38
  • Possible duplicate of https://stackoverflow.com/questions/18371339/how-to-retrieve-name-from-email-address – Aravind S Jul 09 '18 at 11:40
  • i have a issue of name..it fines for longer name.how do i set this pattern in smaller one.I want to do dynamically –  Jul 09 '18 at 11:41
  • Do you know which specific indexes/characters to be replaced? – AB Udhay Jul 09 '18 at 11:48
  • 1
    I suggest you give some examples of input and expected output. When you say you have an issue for short names you don't tell us what you want to happen. Update the question with a few email address examples and what you want to get back. – Reinstate Monica Cellio Jul 09 '18 at 11:53

2 Answers2

2

You've got the start: split into parts and apply an algorithm to the first part to replace some characters, then join the bits again.

The following replaces part of the first part based on a formula that always leaves the last character and replaces at least one of the leading characters. It replaces more of the leading characters as the string gets longer.

Tweak the multipliers that generate the startIndex and endIndex values to replace more or fewer characters.

function mungeEmailAddress(s) {
  var i = s.indexOf('@');
  var startIndex = i * .2 | 0;
  var endIndex   = i * .9 | 0;
  return s.slice(0, startIndex) +
         s.slice(startIndex, endIndex).replace(/./g, '*') +
         s.slice(endIndex);
}

// Testing...
for (var s='', t = '@foo.com', i=97; i<110; i++) {
  s += String.fromCharCode(i);
  console.log(s + t + ' => ' + mungeEmailAddress(s + t));
}
 
RobG
  • 142,382
  • 31
  • 172
  • 209
1

use this :

 var input = "testing@gmail.com";

function callme(){
    var a = input.split("@"); 
    var b = a[0];
    var newstr = "";
    for(var i in b){
     if(i>2 && i<b.length-1)newstr += "*";
     else newstr += b[i];
    }
    console.log(newstr +"@"+ a[1]);
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="callme()">click me</button>
Sidhanshu_
  • 1,714
  • 1
  • 7
  • 10