代码之家  ›  专栏  ›  技术社区  ›  Maciek

Regex删除了额外的空格和换行符,但不是所有的[duplicate]

  •  0
  • Maciek  · 技术社区  · 7 年前

    我正在使用regex删除额外的空格。但我想保留单行换行符。

    '/\s+/'
    

    如何保持单行换行符?

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

    我试过了 '/\s{2,}/'

    1 回复  |  直到 7 年前
        1
  •  0
  •   Community Mohan Dere    5 年前

    这将匹配额外的空格和新行:

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

    (?<=[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