Adam Ilčišák
Adam Ilčišák

Reputation: 647

Regular Expression to capture entire list as one item- javascript

I am working on js filter for string text and I want to use regex to capture entire list of list items, so I can count them and iterate throught them.

Please see what I am trying to achieve: https://regex101.com/r/Jd3JYW/2/

I know how to capture lines separately using this regex (^\*[^\*](.*)$)/gm but I need to capture entire list so I can differentiate between two lists, so I can make each of them start from n.1.

I expected the following code to work ((^\*[^\*](.*)$)+)/gm I thought that this wil capture first item (as it works for separate lines) and also any following untill patern is broken. However it is not working, I am still getting separate lines captured. Do you have any idea guys, how to capture whole list? Thank you.

Upvotes: 1

Views: 263

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627190

You use $ to match line endings, but it is a zero width assertion. You should use some consuming pattern to match line endings, like [\r\n]+:

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

See the regex demo

Details

  • ^ - start of a line
  • (?:^\*[^*\n].*(?:[\r\n]+|$))+ - 1 or more sequences of:
    • \* - a * char
    • [^*\n] - a char other than LF and *
    • .* - any 0+ chars other than line break chars, as many as possible
    • (?:[\r\n]+|$) - either 1+ LF or CR symbols or end of a line (to match the last line).

Upvotes: 2

Related Questions