Created
March 28, 2022 19:08
-
-
Save joshkautz/c374e707cea7369a542f524084720a89 to your computer and use it in GitHub Desktop.
Credit Card detection regex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Detect strings with potential credit cards in them and mask the credit cards. | |
// Credit card strings are strings that have 13-16 consecutive digits. | |
const maskify = (input) => { | |
const regex = /\b\d{13,16}\b/; | |
var card = input.match(regex); | |
return (card) ? mask(input, card[0]) : input; | |
} | |
const mask = (input, card) => { | |
const masked_card = card.slice(0, -4).replace(/./g, '*') + card.slice(-4); | |
return input.replace(card, masked_card); | |
} | |
console.log(maskify('This is my credit card 12345678901 it has Eleven.')); | |
console.log(maskify('This is my credit card 123456789012 it has Twelve.')); | |
console.log(maskify('This is my credit card 1234567890123 it has Thirteen.')); | |
console.log(maskify('This is my credit card 12345678901234 it has Fourteen.')); | |
console.log(maskify('This is my credit card 123456789012345 it has Fifteen.')); | |
console.log(maskify('This is my credit card 1234567890123456 it has Sixteen.')); | |
console.log(maskify('This is my credit card 12345678901234567 it has Seventeen.')); | |
console.log(maskify('This is my credit card 123456789012345678 it has Eighteen.')); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment