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 spa
You could also skip the regex all together.
String testStr = "TESTING THIS OUT WITH DIFFERENT CASES";
String _replaced = testStr.replace(" ", " ");
String replaced = _replaced.replace(" ", " ");
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.