2015-09-07 74 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

相关问题