You can tell replace() to repeat the regex:
.replace(/ /g,’ ’)
G character makes it a “global” match which means it would repeat through the entire string.
If you want to match all whitespace and not just the space character use \s instead:
.replace(/\s/g,’ ‘)
You can also use .replaceAll() like this:
.replaceAll(/\s/g,’ ‘)
Want to learn Java? Check out the Java certification from Intellipaat.