2011-03-15 73 views
1

我想從字符串中替換所有的空格,但是我需要保留新的行字符作爲它?刪除空間並保留新行?

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

india 
aus //both are in differnt line 

是給作爲indaus

我想換行應該保留和刪除的S

+0

'indaus':嗯,哪裏有'ia'走了? – 2011-03-15 11:12:22

+0

i dint get @ @Marcel – user630209 2011-03-15 11:18:30

+0

您的輸入是'india \ naus',您的輸出是'indaus':您可能忘了'ia'。 – 2011-03-15 11:46:58

回答

2

\s指任何空格,換行,包括和標籤。 是一個空間。只刪除空格:

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

你可以刪除「任何空白除了換行符」; most regex flavours count \s as [ \t\r\n],所以我們只是拿出\n\r,你會得到:

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

tht給出了相同的結果。沒有行 – user630209 2011-03-15 11:20:58

+0

@ user630209:沒問題。 – 2011-03-15 11:25:39

4

不能使用此\s(任何空白)。改爲使用字符集:[ \t\f\v]

+0

choiceText = choiceText.replace([\ t \ f \ v]/g,'')。是這樣嗎? – user630209 2011-03-15 11:17:45

+0

是的,只是不要忘記最初的「/」:-) – 2011-03-15 12:44:32