在javascript中屏蔽最后4位数字 [英] Masking last 4 digits in javascript

查看:124
本文介绍了在javascript中屏蔽最后4位数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我使用正则表达式模式来掩盖信用卡号码的最后4位数字

Here I am using regex pattern to mask last 4 digits for Credit Card number

$('#ccnumber').html(ccnbr);  //ccnumber refers to div ID

$('#ccnumber').text(function(_,val) {

            return val.replace(/\d{12}(\d{4})/, "************$1");
        });

仅适用于16位数的信用卡号码。但是,我需要通过 * 来掩盖传入的号码(可能是10或11)并显示最后4位数字。是否有可能在javascript / Jquery?

It is applicable for only 16 digit credit card numbers. But, I need to mask the incoming numbers (may be 10 or 11) by * and show the last 4 digits. Is it possible in javascript/Jquery ?

JSFIDDLE

推荐答案

您可以使用:

str = str.replace(/\d(?=\d{4})/g, "*");

在给定数量超过4位的数字中屏蔽除最后4位数之外的所有数字。

to mask all but last 4 digits in a given number of more than 4 digits.

说明:


  • 此reges使用正向前瞻(?= \ d {4})这意味着匹配后应跟4位数。

  • \d 匹配一个具有以上前瞻条件的数字,并用 *

  • This reges uses a positive lookahead (?=\d{4}) which means match should be followed by 4 digits.
  • \d matches a single digit with above lookahead condition and replaces that by *

这篇关于在javascript中屏蔽最后4位数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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