Vignesh
Vignesh

Reputation: 315

Regex to capture everything but consecutive newlines

What is the best way to capture everything except when faced with two or more new lines?

ex:

name1
address1
zipcode

name2
address2
zipcode

name3
address3
zipcode

One regex I considered was /[^\n\n]*\s*/g. But this stops when it is faced with a single \n character.

Another way I considered was /((?:.*(?=\n\n)))\s*/g. But this seems to only capture the last line ignoring the previous lines.

What is the best way to handle similar situation?

Upvotes: 0

Views: 687

Answers (3)

Vicent
Vicent

Reputation: 5452

UPDATE

You can consider replacing the variable length separator with some known fixed length string not appearing in your processed text and then split. For instance:

> var s = "Hi\n\n\nBye\nCiao";
> var x = s.replace(/\n{2,}/, "#");
> x.split("#");
["Hi", "Bye
Ciao"]

I think it is an elegant solution. You could also use the following somewhat contrived regex

> s.match(/((?!\n{2,})[\s\S])+/g);
["Hi", "
Bye
Ciao"]

and then process the resulting array by applying the trim() string method to its members in order to get rid of any \n at the beginning/end of every string in the array.

Upvotes: 1

Austin Brunkhorst
Austin Brunkhorst

Reputation: 21130

Do you have to use regex? The solution is simple without it.

var data = 'name1...';

var matches = data.split('\n\n');

To access an individual sub section split it by \n again.

//the first section's name
var name = matches[0].split('\n')[0];

Upvotes: 1

Jan Schejbal
Jan Schejbal

Reputation: 4033

((.+)\n?)*(you probably want to make the groups non-capturing, left it as is for readability)

The inner part (.+)\n? means "non-empty line" (at least one non-newline character as . does not match newlines unless the appropriate flag is set, followed by an optional newline)

Then, that is repeated an arbitrary number of times (matching an entire block of non-blank lines).

However, depending on what you are doing, regexp probably is not the answer you are looking for. Are you sure just splitting the string by \n\n won't do what you want?

Upvotes: 1

Related Questions