Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
30
rated 0 times [  34] [ 4]  / answers: 1 / hits: 8817  / 11 Years ago, tue, january 7, 2014, 12:00:00

I have a div that I need to strip away all characters except for the last four. I'm currently using replaceWith for this, but it replaces the whole thing.



Here's what I have



<div class=field-name-field-credit-card>1111111111111111</div>

$(.field-name-field-credit-card).replaceWith(xxxx xxxx xxxx xxxx);


and my result



<div class=field-name-field-credit-card>xxxx xxxx xxxx xxxx</div>


But I would like it to be:



<div class=field-name-field-credit-card>xxxx xxxx xxxx 1111</div>


jsfiddle


More From » jquery

 Answers
49

Well, I made a regex for you and updated your fiddle:



$( .field-name-field-credit-card ).text(function(_,val) {
return val.replace(/d{12}(d{4})/, xxxx xxxx xxxx $1);
});


but I have to agree with Niet - don't mask the credit card client side.



If you really do want to mask client side, this ugly line of code will do it.



What it does is replace the text of the current element with the text it currently has, run through a regex with replace. The regex looks for 12 digits, then stores the next 4, and replaces the string of 16 digits with the 'x's followed by those last 4 it saved.




[#48909] Tuesday, January 7, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
amari

Total Points: 736
Total Questions: 111
Total Answers: 90

Location: Saint Pierre and Miquelon
Member since Fri, Jan 28, 2022
2 Years ago
;