Home > Blockchain >  Regex to check if a string has at least 5 lines [duplicate]
Regex to check if a string has at least 5 lines [duplicate]

Time:10-02

String can contain any number of characters. Of course a \n is the line change we want to count.

How can I check if there are at least 5 \n inside the string?

Putting it in another way: I want to capture the result only if more then 4 \n are present on the string.

Any ideas?

CodePudding user response:

You want 5 or more \n from start (\A) to end (\Z) with anything in between.

\A(.*\n.*){5,}\Z

Note that . does not include \n

CodePudding user response:

As far as I understand this should work, if you validate

const lines = string.match(/(\b. \b)/gm)
if (lines.length > 5) {
   return lines.slice(0, 4).join('\n');
}
return ''; // or any other falsy value


  • Related