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.

Revisions

  1. rpdiss revised this gist Sep 27, 2019. No changes.
  2. rpdiss created this gist Sep 27, 2019.
    8 changes: 8 additions & 0 deletions credit-card-mask.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    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);
    };