Meadow Lizard
Meadow Lizard

Reputation: 350

Regex JS: Matching string between two strings including newlines

I want to match string between two string with Regex including newlines.

For example, I have the next string:

{count, plural,
  one {apple}
  other {apples}
} 

I need to get string between plural, and one. It will be \n*space**space*. I tried this Regex:

/(?:plural,)(.*?)(?:one)/gs

It works, but not in JS. How to do that with JavaScript?

Upvotes: 1

Views: 636

Answers (2)

Hassan Imam
Hassan Imam

Reputation: 22524

To match the everything including newline character, you can use [\s\S] or [^]

var str = `{count, plural,
  one {apple}
  other {apples}
} `;
console.log(str.match(/(?:plural,)([\s\S]*?)(?:one)/g));
console.log(str.match(/(?:plural,)([^]*?)(?:one)/g));

Upvotes: 5

Alex
Alex

Reputation: 360

It doesn´t work because your testing with the wrong regex engine.

 `/s` does not exist in the JS regex engine, only in pcre

It must be something like:

/(?:plural,)((.|\n)*?)(?:one)/g

Hope it helps.

Upvotes: 1

Related Questions