删除空间并保留新线路?

use*_*209 3 javascript regex

我想从字符串中替换所有空格,但我需要保留新的行字符吗?

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

india
aus //both are in differnt line 
Run Code Online (Sandbox Code Playgroud)

正在给予 indaus

我想换行应该保留并删除s

Lig*_*ica 6

\s表示任何空格,包括换行符和制表符。是一个空间。只删除空格:

choiceText=choiceText.replace(/ /g,''); // remove spaces
Run Code Online (Sandbox Code Playgroud)

您可以删除“换行符之外的任何空格”;最正则表达式的口味算\s作为[ \t\r\n],所以我们只取出\n\r,你会得到:

choiceText=choiceText.replace(/[ \t]/g,''); // remove spaces and tabs
Run Code Online (Sandbox Code Playgroud)


Aar*_*lla 6

你不能使用\s(任何空白).改为使用字符集:[ \t\f\v]