tit
tit

Reputation: 619

regex JavaScript with repeating string

I would like to parse with regex files with the following format in order to retrieve content1a, content1b,content2a,content2b, etc ...

===
content1a
===
content1b
===
content2a
===
content2b

Important : the end of file does not contain ===

This regex does almost the job :

/[===[\s\S]*?===[.]*/g 

but does not retrieve the last content (content2b)

Thank you for helping

Upvotes: 2

Views: 61

Answers (2)

The fourth bird
The fourth bird

Reputation: 163632

The pattern that you tried uses a character class, which can also be written as [\s\S]*?=== or ([^]*?)===

It expects === to be there at the end, that is why is does not match the last content.

But if you have for example 5 times an equals sign ===== you will also match the last 2 equals signs, so you could add a newline to prevent that.


Instead of using [\s\S]*? You could use a capturing group to capture all lines that do not start with ===

^===\n((?:(?!===\n).*\n?)*)

Regex demo

const regex = /^===\n((?:(?!===\n).*\n?)*)/gm;
const str = `===
content1a
===
content1b
===
content2a
content2a
content2a
===
content2b`;
let m;

while ((m = regex.exec(str)) !== null) {
  // This is necessary to avoid infinite loops with zero-width matches
  if (m.index === regex.lastIndex) {
    regex.lastIndex++;
  }
  console.log(m[1]);
}

Upvotes: 4

Liam
Liam

Reputation: 29760

You don't need a regex you can just split the string

const str = `===
content1a
===
content1b
===
content2a
===
content2b`;

const contents = str.split('===\n').filter(f => f !== "");

console.log(contents);

Upvotes: 3

Related Questions