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

时间:2018-02-02 23:56:38

标签: javascript

我有以下与数字完美配合,但我也不知道如何让它与字母一起使用。

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;
  }
}

我猜\d目标号码?我不确定在哪里寻找这种事情的参考指南。任何帮助将非常感激!

4 个答案:

答案 0 :(得分:0)

您需要做的就是稍微改变您的正则表达式,以便它也适用于字母。因此,将1更改为(1,2,3),其中:

  • /\d/g匹配a到z范围内的字符以及数字和
  • /[a-z\d]/gi标志确保匹配小写和大写字母。

<强>段:

[a-z\d]

答案 1 :(得分:0)

有效的解决方案是不屏蔽字符串,而是构建一个长度为

的新字符串
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)
}

低于显示的性能优势

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(/[a-z\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')

要获得有关正则表达式效果的更多信息,请阅读codinghorror post

答案 2 :(得分:0)

RegExp参考指南与教程http://www.regular-expressions.info/

RegExp Playground https://regexr.com/(学习,构建和测试正则表达式的在线工具)

如果您需要避免替换短划线-等,请使用单词\w(包括下划线):

&#13;
&#13;
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"
&#13;
&#13;
&#13;

以下是masked在最后一个示例中的变化:

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

没有RegExp示例

这是一个不使用正则表达式(屏蔽任何字符)的示例:

&#13;
&#13;
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"
&#13;
&#13;
&#13;

以下是masked在最后一个示例中的变化:

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

答案 3 :(得分:0)

这是我得到的最短的。您可以使用Regex模式(\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));