简体   繁体   English

使用正则表达式屏蔽 email 地址

[英]Masking of email address using reg expression

Trying to develop masking for email address using reg expression but not able to achieve the desired result.尝试使用正则表达式为 email 地址开发掩码,但无法达到预期的结果。

Input: harrypotter@howarts.com输入:harrypotter@howarts.com

After masking I am looking for result something like this:屏蔽后我正在寻找这样的结果:

Output: "ha*****er@how*****.com"

What I tried is this reg expression:我试过的是这个正则表达式:

(?<=.)[^@\n](?=[^@\n]*?[^@\n]@)|(?:(?<=@.)|(?!^)\G(?=[^@\n]*$)).(?=.*\.)

h*********r@h******.com
const mystring='harrypotter@howarts.com';
mystring.replace(/(^[^@]{2})?(@[^.]{3})?.(?=([^@]{2,}@)|([^@]*\.[^.]+$))/gm,'$1$2*');

This is mostly about what you want to keep, not what you want to mask.这主要是关于你想保留什么,而不是你想掩盖什么。 The approach is to replace every single scanned character with an asterisk one at a time, while skipping the delimiters and the characters around them.该方法是一次用一个星号替换每个扫描的字符,同时跳过定界符和它们周围的字符。

The first two paren sets are optional capture groups that recognize delimiters (begin-string & @) and the 2 or 3 characters to keep after.前两个 paren 集是可选的捕获组,可识别定界符(开始字符串和 @)以及后面要保留的 2 或 3 个字符。 They get put into capture groups 1 & 2 and will be null unless the scanner is at that point in the string.它们被放入捕获组 1 和 2,并且将是 null,除非扫描器位于字符串中的那个点。

The next part .下一部分. is the actual character to be replaced with an asterisk.是要用星号替换的实际字符。

The rest is a lookahead to ensure the characters you want to retain appearing before a delimiter are skipped over. rest 是前瞻性的,以确保您要保留的字符出现在分隔符被跳过之前。 (You only replace if there are at least two chars and one @ somewhere to your right, or if there is no @ and a dot followed by a domain name to your right.) (只有在您右边某处至少有两个字符和一个@,或者没有@和一个点后跟一个域名的情况下,您才需要替换。)

Here it is in action on Regex101:这是在 Regex101 上的作用: 在此处输入图像描述

Your question needs some clarification.你的问题需要澄清一下。 Here is an answer making some assumptions:这是一个做出一些假设的答案:

  • keep 2 leading and trailing chars of the name part (before @ )保留名称部分的 2 个前导和尾随字符( @之前)
  • keep 3 leading chars of the domain part (after @ ) until .保留域部分的 3 个前导字符(在@之后)直到. and TLD (top level domain)和 TLD(顶级域名)
  • replace with three asterisks *** to more effectively obscure the address (better than exact number of chars replaced)替换为三个星号***以更有效地隐藏地址(比替换的确切字符数更好)
  • no replacement for short names and domains.不能替代短名称和域。

Code with examples:代码示例:

 [ `harrypotter@hogwarts.com`, `harry@hogwarts.com`, `tim@hogwarts.com`, `harrypotter@cia.gov` ].forEach(str => { let result = str.replace(/^(..).+?(?=..@)/, '$1***').replace(/(@...).+?(?=\.\w+$)/, '$1***'); console.log(str + ' => ' + result); });

Output: Output:

harrypotter@hogwarts.com => ha***er@hog***.com
harry@hogwarts.com => ha***ry@hog***.com
tim@hogwarts.com => tim@hog***.com
harrypotter@cia.gov => ha***er@cia.gov

Notice that there are two .replace() because one or the other replacement may fail if too short.请注意,有两个.replace()因为如果太短,一个或另一个替换可能会失败。 If you combine into one the whole regex would fail if one side fails.如果你合并成一个,如果一侧失败,整个正则表达式就会失败。

Explanation of first regex:第一个正则表达式的解释:

  • ^ -- anchor at start of string ^ -- 字符串开头的锚点
  • (..) -- capture group 1 for two chars (..) -- 为两个字符捕获组 1
  • .+? -- non-greedy scan for 1+ chars -- 非贪婪扫描 1+ 个字符
  • (?=..@) -- positive lookahead for two chars and @ (?=..@) -- 两个字符和@的正面前瞻

Notice that we could replace the capture group 1 with a positive lookbehind.请注意,我们可以用正向后视替换捕获组 1。 This is however not supported by all browsers, notably Safari, hence better to void.然而,并非所有浏览器都支持此功能,尤其是 Safari,因此最好取消。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM