I am looking for a Java regex way of replacing multiple spaces with non-breaking spaces. Two or more whitespaces should be replaced with the same number of non-breaking spaces, but single whitespaces should NOT be replaced. This needs to work for any number of whitespaces. And the first characters could be 1 or more whitespaces.
So if my String starts off like this:
TESTING THIS OUT WITH DIFFERENT CASES
I need the new String to look like this:
TESTING THIS OUT WITH DIFFERENT CASES
You could also skip the regex all together.
I haven’t tested this but the first one finds all cases of two spaces and replaces them with non-breaking spaces. The second finds cases where there were an odd number of white-spaces and corrects it with two nbsps.