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, "#")
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, "#")
- Is the string of a specific size? – acupofjose Commented Feb 19, 2016 at 16:24
- 2 What if there are less than 4 digits? – Oriol Commented Feb 19, 2016 at 16:26
1 Answer
Reset to default 18use this pattern
\d(?=\d{4})
and replace with #
Demo