I want to search a string and mask the a credit card for security.
"Hi here is my card number its visa 4242 4242 4242 4242"
"Hi here is my card number its visa 4242424242424242"
"Hi here is my card number its visa 4242-4242-4242-4242"
Should be converted to:
"Hi here is my card number its visa **** **** **** 4242"
I need to do this on the client in Javascript. I know there are quite a few resources and questions on the web and SO.
I have found two regex, but both throw errors: "Uncaught SyntaxError: Invalid or unexpected token"
"Hi here is my card number its visa 4242 4242 4242 4242".match("(\d{4}-?){4}")
and
"Hi here is my card number its visa 4242 4242 4242 4242".match(\b4\d{3}[ -]?\d{4}[ -]?\d{4}[ -]?\d{4}[ -]\b)
I think the expressions are not compatible with JS?
I also understand they will return the string, I would then convert the string (mask it) and then use a simple replace on the original string.
Can anybody help me with the regex part of this problem?