2015-09-07 69 views

回答

2

您可以使用regex從字符串去除多餘的\n

\n+將匹配重複一次或多次的所有\ng標誌用於從字符串中刪除所有出現的\n

var foo = "hello\n\n\nworld\n\nfoobar"; 
 
foo = foo.replace(/\n+/g, '\n'); 
 

 
console.log(foo); 
 
alert(foo);

+0

@ teddybear123如果這個答案幫助你,請考慮接受的答案。如果沒有,請在這裏添加評論 – Tushar

相關問題