Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing space and retaining the new line?

i want to replace all the spaces from a string , but i need to keep the new line character as it ?

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

india
aus //both are in differnt line 

is giving as indaus

i want newline should retain and remove the s

like image 392
user630209 Avatar asked Mar 15 '11 11:03

user630209


2 Answers

\s means any whitespace, including newlines and tabs. is a space. To remove just spaces:

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

You could remove "any whitespace except newlines"; most regex flavours count \s as [ \t\r\n], so we just take out the \n and the \r and you get:

choiceText=choiceText.replace(/[ \t]/g,''); // remove spaces and tabs
like image 145
Lightness Races in Orbit Avatar answered Sep 28 '22 13:09

Lightness Races in Orbit


You can't use \s (any whitespace) for this. Use a character set instead: [ \t\f\v]

like image 27
Aaron Digulla Avatar answered Sep 28 '22 12:09

Aaron Digulla