将特定位置的安全编号更改为 X [英] Change security number to X on specific places

查看:19
本文介绍了将特定位置的安全编号更改为 X的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,将数字替换为特定位置的某些字符.我有这组号码 123-45-6789 但我只需要像这样显示 XXX-XX-6789.但我只需要更改不包括破折号"的数字.

I am new in replacing number into some characters in specific places. I have this set of number 123-45-6789 but I need to show only like this XXX-XX-6789. But I need to change only the number not including the "dash".

下面是我的示例代码

var mainStr = $("#view_ssn").text(),
    vis = mainStr.slice(-4),
    countNum = '';

for(var i = (mainStr.length)-4; i>0; i--){
    countNum += 'X';
}
$("#view_ssn").text(countNum+vis);

输出

XXXXXXX6789

XXXXXXX6789

推荐答案

您可以简单地使用:

var ssn = "123-45-6789";
var output = ssn.replace(/\d(?=.{5,})/g, "X");
console.log(output);

正则表达式模式 \d(?=.{5,}) 背后的逻辑是替换输入社会安全号码中的每个数字,该数字前面至少有 5 个字符.这不包括 SSN 的最后 4 位数字.

The logic behind the regex pattern \d(?=.{5,}) is to replace every digit in the input Social Security number which has at least 5 characters in front of it. This excludes the last 4 digits of the SSN.

这篇关于将特定位置的安全编号更改为 X的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆