简体   繁体   English

如何屏蔽字符串的字母和数字

[英]How to mask letters and numbers of a string

I have the following that works perfectly with numbers but I don't know how to get it to work with letters too.我有以下与数字完美搭配的内容,但我也不知道如何让它与字母一起使用。

var word = "4556364607935616";

function mask() {
  if (word.length <= 4) {
    return word;
  } else {
    var masked =
      word.substring(0, word.length - 4).replace(/\d/g, "#") +
      word.substring(word.length - 4, word.length);
    return masked;
  }
}

I'm guessing \\d targets numbers?我猜\\d目标数字? I'm not sure where to look for a reference guide for this kind of thing.我不确定在哪里可以找到这种事情的参考指南。 Any help would be much appreciated!任何帮助将不胜感激!

All you need to do is alter your regular expression slightly so that it works for letters as well.您需要做的就是稍微改变您的正则表达式,使其也适用于字母。 So, change /\\d/g to /[az\\d]/gi , where:因此,将/\\d/g更改为/[az\\d]/gi ,其中:

  • [az\\d] matches a character in the range of a to z as well as numbers and [az\\d]匹配 a 到 z 范围内的字符以及数字和
  • the i flag ensures that both lowercase and uppercase letters are matched. i标志确保小写和大写字母都匹配。

Snippet:片段:

 var word = "4f563a46jy7u35616"; function mask() { if (word.length <=4) { return word } else { var masked = word.substring(0, word.length - 4).replace(/[az\\d]/gi,"#") + word.substring(word.length - 4, word.length) return masked; } } console.log(mask(word));

The effective solution would be to not mask the string, but build a new string with provided length有效的解决方案是不屏蔽字符串,而是构建一个具有提供长度的新字符串

var word = "4556364607935616";

function mask() {
  var LENGTH = 4
  var CHAR = '#'
  if (word.length <= LENGTH) {
    return word
  }
  var leftSideLength = word.length - LENGTH
  var result = ''
  while (leftSideLength--) result += CHAR
  return result + word.substring(word.length - 4, word.length)
}

Below the performance advantage shown下面显示的性能优势

 var word = "4556364607935616"; function maskNotRegex() { var LENGTH = 4 var CHAR = '#' if (word.length <= LENGTH) { return word } var leftSideLength = word.length - LENGTH var result = '' while (leftSideLength--) result += CHAR return result + word.substring(word.length - 4, word.length) } // Credit: Angel Politis's anwer on this Post function maskRegex() { if (word.length <= 4) { return word } else { var masked = word.substring(0, word.length - 4).replace(/[az\\d]/gi, "#") + word.substring(word.length - 4, word.length) return masked; } } // Performance test // Credit: https://stackoverflow.com/a/17943511/2308005 var iterations = 1000000; console.time('Using regex'); for (var i = 0; i < iterations; i++) { maskRegex(word); }; console.timeEnd('Using regex') console.time('Not using regex'); for (var i = 0; i < iterations; i++) { maskNotRegex(word); }; console.timeEnd('Not using regex')

To get more info about regex performance, read codinghorror post要获取有关正则表达式性能的更多信息,请阅读codinghorror 帖子

RegExp Reference Guide with Tutorials : http://www.regular-expressions.info/带有教程的 RegExp 参考指南http : //www.regular-expressions.info/

RegExp Playground : https://regexr.com/ (online tool to learn, build, & test regular expressions) RegExp Playgroundhttps : //regexr.com/ (学习、构建和测试正则表达式的在线工具)

If you need to avoid replacing dashes - etc. use the word character \\w (includes underscore):如果您需要避免替换破折号-等,请使用单词字符\\w (包括下划线):

 var word="abc-364-079-5616", masked=word.replace(/\\w/g, "#"); //[a-zA-Z0-9_] if(word.length > 4) { masked = masked.substring(0, word.length-4) + word.substring(word.length-4); } else { masked = word; } console.log(masked); // "###-###-###-5616"

Here's how masked changes in the last example:以下是上一个示例中masked更改的方式:

  • masked = "###-###-###-####"
  • masked = "###-###-###-5616"

No RegExp Example没有正则表达式示例

Here's an example that doesn't use regular expressions (masks any character):这是一个不使用正则表达式(屏蔽任何字符)的示例:

 var word = "abc6364607935616", masked = word; // word & masked are the same if(word.length > 4) { masked = new Array(word.length - 4).join('#'); // create 4 less than word masked += word.substring(word.length - 4); // add on 4 last characters } console.log(masked); // "###########5616"

Here's how masked changes in the last example:以下是上一个示例中masked更改的方式:

  • masked = "abc6364607935616"
  • masked = "############"
  • masked = "############5616"

This is the shortest I got.这是我得到的最短的。 You can use Regex pattern ( \\w(?=\\w{4}) ) to mask the last 4 letters like so:您可以使用正则表达式模式( \\w(?=\\w{4}) )来屏蔽最后 4 个字母,如下所示:

 let word = "4f563a46jy7u35616"; function mask() { if (word.length <= 4) { return word; } else { return word.replace(/\\w(?=\\w{4})/g, "#"); } } console.log(mask(word));

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

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