function maskify(cc) {
var dd = cc.toString();
var hash = dd.replace((/./g), '#');
for (var i = (hash.length - 4); i < hash.length; i++) {
hash[i] = dd[i];
}
return hash;
}
I am trying to replace all chars with #
except for last 4. Why isn't it working?
Summary. To replace all occurrences of a substring in a string by a new one, you can use the replace() or replaceAll() method: replace() : turn the substring into a regular expression and use the g flag. replaceAll() method is more straight forward.
The replaceAll() method returns a new string with all matches of a pattern replaced by a replacement . The pattern can be a string or a RegExp , and the replacement can be a string or a function to be called for each match.
Use the replace() method to replace multiple characters in a string, e.g. str. replace(/[. _-]/g, ' ') . The first parameter the method takes is a regular expression that can match multiple characters.
You could do it like this:
dd.replace(/.(?=.{4,}$)/g, '#');
var dd = 'Hello dude';
var replaced = dd.replace(/.(?=.{4,}$)/g, '#');
document.write(replaced);
If you find the solution, try this trick
function maskify(cc) {
return cc.slice(0, -4).replace(/./g, '#') + cc.slice(-4);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With