You want to match an empty string and replace it with "*" . So something like this works:
System.out.println("string".replaceAll("", "*"));
Or even better, since an empty string can be matched literally without a regular expression, you can simply do:
System.out.println("string".replace("", "*"));
Why does it work
This is because any instance of the string startsWith("") and endsWith("") and contains(""). Between any two characters in any line there is an empty line. In fact, there are an infinite number of empty lines in these places.
(And yes, this is true for the empty line itself. This is the "empty" line contains itself!).
The regex mechanism and String.replace automatically advance the index when looking for the next match in these cases, to prevent an infinite loop.
"Real" regex
There is no need for this, but it is shown here for educational purposes: something like this also works:
System.out.println("string".replaceAll(".?", "*$0")); // "*s*t*r*i*n*g*"
This works by matching "any" character with . and replacing it with * and this character, by calling back to group 0.
To add an asterisk for the last character, we allow . be optional with .? . Does it work because ? is greedy and always accepts a character, if possible, i.e. Anywhere except the last character.
If the string can contain newline characters, use Pattern.DOTALL/(?s) mode.
References