Here it goes: I have a sentence in a Textfield form and I need to reprint the content of a sentence but WITHOUT spaces.
For example: "My name is Slavisha" The result: "MynameisSlavisha"
To remove a space character, you'll want to replace it with an empty character:
Replacing a Space:
Let's say, for example, that you're saving the text from the text field into a variable named
strInput.replace(' ', ''); will find the first space character and replace it with nothing (effectively removing it).
Replacing All Spaces:
You probably don't just want to remove one space character, though, you want to remove all of them. To do that, you'll need to use the replace function to replace all occurrences of the substring. To do this, you use the regular expression
/ /g instead of
' ', so your expression becomes
strInput.replace(/ /g, '');. The "g" is the part that finds all occurrences.
Replacing All Whitespaces:
This would probably take care of what you want to do, but you might want to remove all white space characters, including new line characters. For this, you would use
\s in your regular expression instead of a space. The final result is:
Here's the jsFiddle version if you want to mess around with it: http://jsfiddle.net/qXyEw/