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
\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
You can't use \s
(any whitespace) for this. Use a character set instead: [ \t\f\v]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With