RegExp Reference Guide with Tutorials: http://www.regular-expressions.info/
RegExp Playground: https://regexr.com/ (online tool to learn, build, & test regular expressions)
If you need to avoid replacing dashes -
etc. use the word character \w
(includes underscore):
var word="abc-364-079-5616", masked=word.replace(/\w/g, "#"); //[a-zA-Z0-9_]
if(word.length > 4) {
masked = masked.substring(0, word.length-4) + word.substring(word.length-4);
} else {
masked = word;
}
console.log(masked); // "###-###-###-5616"
Here's how masked
changes in the last example:
masked = "###-###-###-####"
masked = "###-###-###-5616"
No RegExp Example
Here's an example that doesn't use regular expressions (masks any character):
var word = "abc6364607935616", masked = word; // word & masked are the same
if(word.length > 4) {
masked = new Array(word.length - 4).join('#'); // create 4 less than word
masked += word.substring(word.length - 4); // add on 4 last characters
}
console.log(masked); // "###########5616"
Here's how masked
changes in the last example:
masked = "abc6364607935616"
masked = "############"
masked = "############5616"