Replace All Characters In Specific Group To Asterisks
I found this SO post that is close, but I want to expand on this question a bit. I need to replace each character (within a group of the regex) to an asterisk. For example Hello, m
Solution 1:
You can use a replacement function with String.prototype.replace
which will get the matching text and groups.
var input = 'Hello, my password is: SecurePassWord';
var regex = /(Hello, my password is: )(\w+)/;
var output = input.replace(regex, function(match, $1, $2) {
// $1: Hello, my password is: // $2: SecurePassWord// Replace every character in the password with asterisksreturn $1 + $2.replace(/./g, '*');
});
console.log(output);
Post a Comment for "Replace All Characters In Specific Group To Asterisks"