Removing space and saving a new line?

I want to replace all spaces from a string, but I need to save the new character of the string as?

choiceText=choiceText.replace(/\s/g,'');

india
aus //both are in differnt line 

gives as indaus

I want the newline character to save and delete s

+3
source share
2 answers

You cannot use \s(any spaces) for this. Use the character set instead:[ \t\f\v]

+5
source

\smeans any spaces, including newlines and tabs. - space. To remove spaces:

choiceText=choiceText.replace(/ /g,''); // remove spaces

You can remove "any spaces except newlines"; most regex count expressions are \slike[ \t\r\n] , so we just take out \nboth \rand you get:

choiceText=choiceText.replace(/[ \t]/g,''); // remove spaces and tabs
+4

All Articles