Skip to content

Instantly share code, notes, and snippets.

@ceautery
Created April 20, 2016 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ceautery/5b32a46ac4ed6c1df5d63281df90ccd9 to your computer and use it in GitHub Desktop.
Save ceautery/5b32a46ac4ed6c1df5d63281df90ccd9 to your computer and use it in GitHub Desktop.
Mask the middle part of credit card numbers
function MiddleMasker(leadNumChars, trailNumChars, replaceChar) {
var re = new RegExp('(^.{' + leadNumChars + '})?.(?=.{' + trailNumChars + '})', 'g');
this.mask = function(str) {
return str.replace(re, '$1' + replaceChar)
}
}
var m = new MiddleMasker(6, 4, 'X');
m.mask('4111123456789012'); // "411112XXXXXX9012"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment