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(
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.
Explanation:
(?=\d{4}) which means match should be followed by 4 digits.\d matches a single digit with above lookahead condition and replaces that by *