Skip to content Skip to sidebar Skip to footer

\z Pcre Equivalent In Javascript Regex To Match All Markdown List Items

I'm trying to parse a Markdown style list into HTML. I am using several Regular Expressions for this, all according to the JavaScript standard. I know there are several different t

Solution 1:

If you want to match the very end of string position in a JavaScript regex that has a m flag you may use $(?![^]) or $(?![\s\S]) like pattern. Your pattern will look like

/^(?:\d.|[*+-]) [^]*?(?=^(?:\d.|[*+-])|$(?![^]))/gm
                                       ^^^^^^^^ 

See the regex demo. The $(?![^]) (or $(?![\s\S])) matches the end of a line that has no other char right after it (so, the very end of the string).

However, you should think of unrolling the lazy dot part to make the pattern work more efficiently.

Here is an example:

/^(?:\d+\.|[*+-]) .*(?:\r?\n(?!(?:\d+\.|[*+-]) ).*)*/gm

See the regex demo

Details

  • ^ - start of a line
  • (?:\d+\.|[*+-]) - 1+ digits and a dot or a * / + / -
  • - a space
  • .* - any 0+ chars other than line break chars as many as possible
  • (?:\r?\n(?!(?:\d+\.|[*+-]) ).*)* - 0 or more sequences of a CRLF or an LF line ending not followed with - 1+ digits and a dot or a * / + / - followed with a space and then the rest of the line.

Post a Comment for "\z Pcre Equivalent In Javascript Regex To Match All Markdown List Items"