-1

I am still struggling learning my way around regex in Javascript. I'm trying to create a markdown converter that will convert like the following:

> this text should be matched --> <span>this text should be matched</span>

I've got that down, but I'm trying to make it more complex, as shown below:

> this is a match
> so is this, but should be in the same match as above

> this should be a seperate match > this is nothing

would equal:

<span>this is a match so is this, but should be in the same match as above</span>
<span>this should be a seperate match > this is nothing</span>

But the issue I am having is that using the regex I currently have (/^\>(.*?)(?=\>|\n\n)/gm), it is picking up the first two as seperate matches.

See my example here: https://regex101.com/r/cZkJAT/2

Any help would be great, thanks.

Zac Webb
  • 653
  • 6
  • 22

1 Answers1

0

You are looking for the dotall modifier for your regex, which would force dot to match across lines, which it does not do by default. This does not exist for JS regex, but as a workaround you can rephrase your regex as the following:

(/^\>([\s\S]*?)(?=\>|\n\n)/gm

Also as a quick note about line endings, Unix uses \n as you have in your pattern, but Window uses \r\n. To match a line ending in either you could use \r?\n.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360