admin管理员组

文章数量:1334663

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? I'm not sure where to look for a reference guide for this kind of thing. Any help would be much appreciated!

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? I'm not sure where to look for a reference guide for this kind of thing. Any help would be much appreciated!

Share Improve this question edited Feb 21, 2020 at 15:30 Penny Liu 17.5k5 gold badges86 silver badges108 bronze badges asked Feb 2, 2018 at 23:56 letoleto 5991 gold badge6 silver badges20 bronze badges 1
  • A reference guide for this kind of thing. – AuxTaco Commented Feb 3, 2018 at 0:02
Add a ment  | 

5 Answers 5

Reset to default 2

All you need to do is alter your regular expression slightly so that it works for letters as well. So, change /\d/g to /[a-z\d]/gi, where:

  • [a-z\d] matches a character in the range of a to z as well as numbers and
  • the i flag ensures that both lowercase and uppercase letters are matched.

Snippet:

var word = "4f563a46jy7u35616";

function mask() {
  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;
  }
}

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(/[a-z\d]/gi, "#") +
      word.substring(word.length - 4, word.length)
    return masked;
  }
}

// Performance test
// Credit: https://stackoverflow./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

RegExp Reference Guide with Tutorials: http://www.regular-expressions.info/

RegExp Playground: https://regexr./ (online tool to learn, build, & test regular expressions)

If you need to avoid replacing dashes - etc. use the word character \w (includes underscore):

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 = "###-###-###-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 = "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:

let word = "4f563a46jy7u35616";

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

console.log(mask(word));

If you want to show only end of the string and mask the remaining part, you could use this solution:

Input : "1234567890"

Output : #####67890

const str = '1234567890'

const maskString = (string = '', maskCharacter = '#') => {
  const maskLength = string.length <= 10 ? string.length / 2 : string.length - 5
  return Array(Math.round(maskLength)).fill(maskCharacter).join('') + string.substring(maskLength)
}

console.log(maskString(str))

本文标签: javascriptHow to mask letters and numbers of a stringStack Overflow