-1
我想一次只限制换行符为1。只允许在一个字符串中有一个换行符javascript
var foo = "hello\n\n\nworld\n\nfoobar"
期望的结果:
var foo = "hello\nworld\nfoobar"
我想一次只限制换行符为1。只允许在一个字符串中有一个换行符javascript
var foo = "hello\n\n\nworld\n\nfoobar"
期望的结果:
var foo = "hello\nworld\nfoobar"
您可以使用regex
从字符串去除多余的\n
。
\n+
将匹配重复一次或多次的所有\n
。 g
标志用于从字符串中删除所有出现的\n
。
var foo = "hello\n\n\nworld\n\nfoobar";
foo = foo.replace(/\n+/g, '\n');
console.log(foo);
alert(foo);
@ teddybear123如果这个答案帮助你,请考虑接受的答案。如果没有,请在这里添加评论 – Tushar