use*_*256 6 javascript regex jquery
我们为新员工提供入职表格,其中包含需要剥离的多个换行符(行间距为4-5行).我想摆脱额外的新行,但仍然用一个\n来区分块.
例:
New employee<br/>
John Doe
Employee Number<br/>
1234
Run Code Online (Sandbox Code Playgroud)
我目前正在使用text = text.replace(/(\r\n|\r|\n)+/g, '$1');但是没有间距就摆脱了所有新行.
ael*_*lor 15
text = text.replace(/(\r\n|\r|\n){2,}/g, '$1\n');
Run Code Online (Sandbox Code Playgroud)
使用它,它将删除至少有2个或更多的换行符
更新
关于OP的具体要求我会稍微编辑一下答案.
text = text.replace(/(\r\n|\r|\n){2}/g, '$1').replace(/(\r\n|\r|\n){3,}/g, '$1\n');
Run Code Online (Sandbox Code Playgroud)
我们可以整理正则表达式,如下所示:
text = text.replace(/[\r\n]{2,}/g, "\n");
Run Code Online (Sandbox Code Playgroud)