我想从字符串中替换所有空格,但我需要保留新的行字符吗?
choiceText=choiceText.replace(/\s/g,'');
india
aus //both are in differnt line
Run Code Online (Sandbox Code Playgroud)
正在给予 indaus
我想换行应该保留并删除s
\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)