最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - I need Regex to mask email address in javascript - Stack Overflow

programmeradmin0浏览0评论

I want to mask email address [email protected]=> [email protected] in JavaScript

i used /(?<=.{2}).(?=[^@]*?@)/ regex but not working in internet explorer and Mozilla so i need regex which works in all browser(JavaScript)

maskedEmail = stringObj.replace(/(?<=.{2}).(?=[^@]*?@)/g, "X");

I want to mask email address [email protected]=> [email protected] in JavaScript

i used /(?<=.{2}).(?=[^@]*?@)/ regex but not working in internet explorer and Mozilla so i need regex which works in all browser(JavaScript)

maskedEmail = stringObj.replace(/(?<=.{2}).(?=[^@]*?@)/g, "X");

Share Improve this question edited Jul 6, 2019 at 10:05 Parth Patel 91913 silver badges35 bronze badges asked Jul 6, 2019 at 10:00 KhonKhon 2755 silver badges15 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Try this option, which avoids using a lookbehind:

var email = "[email protected]";
var output = email.replace(/^(.{2})[^@]+/, "$1XXX");
console.log(email);
console.log(output);

Note that I took a slight shortcut here by always representing the third character of the email name onwards with static XXX. We could try to keep the same length name, but it would take more work. I actually advise against doing that, because it partially defeats the purpose of masking the email, by giving away the actual length.

There is no need of regex for this simple operation. This should work equally well:

var emailAddress = "[email protected]"
    
    function maskEmail(email) {
      let split = email.split('@')
      return email.substr(0,1) + new Array(split[0].length - 1).fill('x').join('') + "@" + split[1]
    }
    
    console.log(maskEmail(emailAddress))

发布评论

评论列表(0)

  1. 暂无评论