'Regular expression to replace inner credit card numbers with X's?
Is it possible to use regex to replace all characters in a card number with an X, except for the left-outer 4-digits and right-outer 4-digits, when the card number varies in length between 15-19 characters?
I've been searching but couldn't find a solution with variable card lengths.
I'm building a sails.js app.
(Note: For those of you with PCI-compliance worries, I'm not storing credit card numbers and it's not a live application)
Solution 1:[1]
use this pattern
(?<=\d{4})\d(?=\d{4})
and replace w/ X
Demo
(?<= # Look-Behind
\d # <digit 0-9>
{4} # (repeated {4} times)
) # End of Look-Behind
\d # <digit 0-9>
(?= # Look-Ahead
\d # <digit 0-9>
{4} # (repeated {4} times)
) # End of Look-Ahead
if your engine does not support look-behind use this pattern instead
^(\d{4})\d(?=\d{4})|\d(?=\d{4})
Note: will only work properly for 9+ digit numbers Demo
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |