Home > Net >  javascript regex detect duplicate blank lines
javascript regex detect duplicate blank lines

Time:10-15

I tried to detect duplicate blank lines with

\s*\n\n 

https://regex101.com/r/v0imUZ/1

but doesn't seem to work with

  test1
   
    
   
   test2
   test3
  test4

  test1
   
    
   
   test2
   test3
  test4

CodePudding user response:

As commented,

A simpler way would be /\n{2,}/ or /\n[^\S] /

you can try /\n[^\S]*\n/.

Idea is to check for new line, optionally followed by whitespace character followed by a new line.

CodePudding user response:

You can use

\n(?:[^\S\n]*\n) 

See the regex demo. If there can be CRLF endings:

\r?\n(?:[^\S\n\r]*\r?\n) 

Details:

  • \r? - an optional carriage return symbol
  • \n - a newline char
  • (?:[^\S\n]*\n) - one or more occurrences of
    • [^\S\n]* - zero or more whitespace chars excluding newline char, and then
    • \n - a newline char.
  • Related