1

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.

3
  • @WiktorStribiżew this works. Thank you! Commented Feb 26, 2018 at 20:19
  • Adam, I posted a variation of the regex that also supports the last line as a part of the list. Commented Feb 26, 2018 at 20:27
  • Thank you Wiktor. I really appreciate your help as well as explanation :) Commented Feb 26, 2018 at 20:31

1 Answer 1

2

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).
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.