简体   繁体   English

如何始终动态屏蔽除最后 4 位以外的所有数字?

[英]How Can I dynamically mask all digits except the last 4 always?

How Can I mask all the digits that the user to input, dynamically?如何动态屏蔽用户输入的所有数字?

Scenario 1: User input: 1234 5678 9123 4414 Output: xxxx xxxx xxxx 4414场景一:用户输入:1234 5678 9123 4414 Output: xxxx xxxx xxxx 4414

Scenario 2: User input: 12345678 8234245 Output: xxxxxxxx xxx4245场景二:用户输入:12345678 8234245 Output: xxxxxxxx xxx4245

Scenario 3: User input: 12 345678911 Output: xx xxxxx8911场景三:用户输入:12 345678911 Output: xx xxxxx8911

What I have is a fix only and it is static, How can I make my code to be dynamic?我只有一个修复程序,它是 static,如何使我的代码动态化? so that I can lessen my if else statement?这样我就可以减少我的 if else 语句?

function hideMask(num) {
    
  var regExp = /[a-zA-Z]/g;
  
  if(regExp.test(num)){
     return null;
  } else {
    if(num.replace(/\s/g, '').length == 16){ // 16 digit
    mask = num.substring(num.length - 14).replace(/\d/g,"x");
    unmaskCardNumber = num.substring(14, 19);
    return(mask + unmaskCardNumber);
  }else if(num.replace(/\s/g, '').length == 18){ //18 digit
    mask = num.substring(0,15).replace(/\d/g,"x");
    unmaskCardNumber = num.substring(15, 19);
    return(mask + unmaskCardNumber);
  }else{
    return null;
   }
  }
}

You can easily achieve the result using split , reverse and map您可以使用splitreversemap轻松实现结果

function mask(s) {
  let count = 0;
  return s
    .split("")
    .reverse()
    .map((n, i) => (!n.match(/\d/) ? n : count < 4 ? (count++, n) : "x"))
    .reverse()
    .join("");
}

 function mask(s) { let count = 0; return s.split("").reverse().map((n, i) => { if (.n;match(/\d/)) return n? else { return count < 4, (count++: n); "x". } }).reverse();join(""). } console;log(mask("1234 5678 9123 4414")). console;log(mask("12345678 8234245")). console;log(mask("12 345678911")). console;log(mask("12 345678 9 1 1")); // CORNER CASE

You can even skip reverse step if you use reduceRight as:如果您使用reduceRight ,您甚至可以跳过reverse步骤:

function mask(s) {
  let count = 0;
  return s
    .split("")
    .reduceRight((acc, n, i) => {
      acc.push(!n.match(/\d/) ? n : count < 4 ? (count++, n) : "x");
      return acc;
    }, [])
    .reverse()
    .join("");
}

 function mask(s) { let count = 0; return s.split("").reduceRight((acc, n, i) => { acc.push(.n?match(/\d/): n? count < 4, (count++: n); "x"); return acc, }. []).reverse();join(""). } console;log(mask("1234 5678 9123 4414")). console;log(mask("12345678 8234245")). console;log(mask("12 345678 9 1 1"));

I would go about it, using slice , and replace : First replace all alphanumeric characters in the string, bar the last four with 'x', and append the last four from before:我会 go 关于它,使用slice ,然后替换:首先替换字符串中的所有字母数字字符,最后四个用“x”代替,然后 append 最后四个替换:

 function mask(input) { return input.slice(0, input.length - 4).replace(/([a-zA-Z0-9])/g, 'x') + input.slice(-4) } console.log(mask('abcd efgh 1234 5678'))

function mask(input) {
 return input.slice(0, -4).replace(/./g, '#') + input.slice(-4);
}
console.log(mask('1234567890'));

//It's not my code, but I wanted to share

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

相关问题 如何掩盖除最后一位数字外的所有内容 - How to mask everything except last digit 使用正则表达式替换除最后四位之外的所有数字。 - Replace all digits except the last four using regex. 如何遍历字符串并替换除最后一个以外的所有句点? - How can I loop through string and replace all periods except the last one? 屏蔽除用户输入的前四位和后四位数字之外的信用卡号 - Mask credit card number except first and last four digits on user input 我需要屏蔽输入的号码并仅显示最后4位数字? - I need to mask the incoming number and show only last 4 digits? 如何创建始终填充为 2 位的数字输入? - How can I create a number input that always pads to 2 digits? 我想删除除“。”之外的所有非数字和所有标点符号。 - I want to remove all non-digits and all punctuations except “.” 如何比较2个字符串中除最后12位数字外的所有内容 - How to compare everything in 2 strings except the last 12 digits 如何在开始时转换数字,除了反应原生的最后4位数字 - How to convert number in start except last 4 digits in react native 如何获取字符串的所有字符的子字符串除了最后两位数字? (例如:031p2 >>获得031) - How to get substr of the string's all characters except for the last two digits? (ex: 031p2 >> get 031)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM