Aaron
Aaron

Reputation: 4480

Replace all digits except the last four using regex.

I am using regex in javascript and want to replace all the digits in a number except the last four with '#'. I have figured out how to replace all the digits with '#', what do I do to not replace the last four? Here is my code so far. return cc.replace(/[0-9]/g, "#")

Upvotes: 9

Views: 8585

Answers (1)

alpha bravo
alpha bravo

Reputation: 7948

use this pattern

\d(?=\d{4})

and replace with #
Demo

Upvotes: 18

Related Questions