Skip to content

Instantly share code, notes, and snippets.

@rpdiss
Created September 27, 2019 12:13
Show Gist options
  • Select an option

  • Save rpdiss/04a37ffde5156ceaf3e1ac5c981ee4b7 to your computer and use it in GitHub Desktop.

Select an option

Save rpdiss/04a37ffde5156ceaf3e1ac5c981ee4b7 to your computer and use it in GitHub Desktop.
typescript credit card masking function
export const maskCreditCard = async (creditCard: string, start: number, end: number, mask = `*`): Promise<string> => {
const pattern = `(?<=[0-9]{${start}})(?:.*:?)(?=[0-9]{${end}})`;
const regExp = new RegExp(pattern, 'g');
const startRes = creditCard.substr(0, start);
const endRes = creditCard.substr(creditCard.length - end);
const maskMatch = '4505290686046367'.match(regExp);
return maskMatch ? `${startRes}${maskMatch[0].replace(/[a-zA-Z0-9]/g, mask)}${endRes}` : creditCard.replace(/[a-zA-Z0-9]/g, mask);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment