Masking last 4 digits in JavaScript

后端 未结 7 1690
遥遥无期
遥遥无期 2020-12-09 05:39

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(         


        
7条回答
  •  我在风中等你
    2020-12-09 05:57

    You can use:

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

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

    RegEx Demo

    Explanation:

    • 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 *

提交回复
热议问题