用javascript掩盖电子邮件

时间:2016-08-31 11:03:13

标签: javascript jquery

我是javascript的新手,我想在js中掩盖电子邮件ID

mynewemail@gmail.com 应掩盖为 m ******** l@gmail.com 。我如何在js中实现。我的下面的代码不适用于浏览器

 var maskid = "";
var myemailId =  "mynewemail@gmail.com";
var prefix= myemailId .substring(0, myemailId .lastIndexOf("@"));
var postfix= myemailId .substring(myemailId .lastIndexOf("@"));

for(var i=0; i<prefix.length; i++){
    if(i == 0 || i == prefix.length-1 ) {
        maskid = maskid + prefix[i].toString();
    }
    else {
        maskid = maskid + "*";
    }
}
maskid =maskid +postfix;

我想在JS中处理是必需的。

由于

6 个答案:

答案 0 :(得分:11)

您可以使用基于正则表达式的替换:

var maskid = myemailId.replace(/^(.)(.*)(.@.*)$/,
     (_, a, b, c) => a + b.replace(/./g, '*') + c
);

小心:

  • 做服务器端,因为任何客户端替换都可能被用户推翻。
  • 并非所有电子邮件地址都符合您的要求
  • 当匹配时,它也可能不会隐藏太多

答案 1 :(得分:2)

试试这个:

var maskid = "";
var myemailId =  "mynewemail@gmail.com";
var prefix= myemailId.substring(0, myemailId .lastIndexOf("@"));
var postfix= myemailId.substring(myemailId .lastIndexOf("@"));

for(var i=0; i<prefix.length; i++){
    if(i == 0 || i == prefix.length - 1) {   ////////
        maskid = maskid + prefix[i].toString();
    }
    else {
        maskid = maskid + "*";
    }
}
maskid =maskid +postfix;

console.log(maskid)

答案 2 :(得分:1)

let str = "tonkjhuhuhuy@gmail.com"
str = str.split('');
let finalArr=[];
let len = str.indexOf('@');
str.forEach((item,pos)=>{
(pos>=1 && pos<=len-2) ? finalArr.push('*') : finalArr.push(str[pos]);
})
console.log(finalArr.join(''))

答案 3 :(得分:0)

这是另一种方法。

&#13;
&#13;
var maskedid = "";
var myemailId = "mynewemail@gmail.com";
var index = myemailId.lastIndexOf("@");
var prefix = myemailId.substring(0, index);
var postfix = myemailId.substring(index);

var mask = prefix.split('').map(function(o, i) {
  if (i == 0 || i == (index - 1)) {
    return o;
  } else {
    return '*';
  }
}).join('');

maskedid = mask + postfix;

console.log(maskedid);
&#13;
&#13;
&#13;

答案 4 :(得分:0)

我验证了IE 8中的代码。代码共享按预期工作。请分享您检查的版本。同时确保选中“启用复选框允许活动内容在我的电脑上的文件中运行”。

步骤: 1)从Internet Explorer中,选择“工具”菜单,然后选择“Internet选项”。 2)在“Internet选项”对话框中,选择“高级”选项卡。 向下滚动到“安全”选项。启用复选框允许活动内容在“我的电脑”上的文件中运行。

答案 5 :(得分:0)

  function getMsakedEmail(email){
    let skipFirstChars = 3;
    let firstThreeChar = email.slice(0, skipFirstChars);

    let domainIndexStart = email.lastIndexOf("@");
    let maskedEmail = email.slice(skipFirstChars, domainIndexStart)
    maskedEmail = maskedEmail.replace(/./g, '*')
    let domain = email.slice(domainIndexStart, email.length);

    return firstThreeChar.concat(maskedEmail).concat(domain);
  }

  function getMsakedEmail2(email){
    let skipFirstChars = 3;
    let firstThreeChar = email.slice(0, skipFirstChars);

    let domainIndexStart = email.lastIndexOf("@");
    let maskedEmail = email.slice(skipFirstChars, domainIndexStart-1)
    maskedEmail = maskedEmail.replace(/./g, '*')
    let domainPlusPreviousChar = email.slice(domainIndexStart-1, email.length);

    return firstThreeChar.concat(maskedEmail).concat(domainPlusPreviousChar);
  }

  let email = "abcdefgh@gmail.com";
  console.log(getMsakedEmail(email));
  console.log(getMsakedEmail2(email));