Inside textareas and html elements, there are several types of white space. There are line feeds, carriage returns, and of course, the space. But there are also other spaces.
And in particular compressed spaces.
If you’re going to process strings that contain sequences of white space, it might be worthwhile to make sure your html doesn’t compress them into a special whitespace character. This would allow methods like replace to work as expected:
str.replace(/ /g,"");