正则表达式删除其他空格和换行符,但不是全部

时间:2018-09-23 03:49:14

标签: regex

我正在使用正则表达式删除其他空格。但是我想保留单个换行符。

'/\s+/'

如何保持单个换行符?

仅删除多余的空格和换行符。

我已经尝试过'/\s{2,}/',但这仍然消除了我的换行符。

1 个答案:

答案 0 :(得分:0)

这将匹配多余的空格和换行符:

(?<= ) | +$|(?<=\n)\n

说明(用“ [space]”代替空格):

(?<=[space])[space] | Match any space that has a preceding space
|                   | or
[space]+$           | Match one or more consecutive spaces at the end of a line
|                   | or
(?<=\n)\n           | Match any new line that has a preceding new line

此表达式应使用全局和多行标志。只需将火柴替换为空。

Try it here

相关问题