Search code examples
javascriptregexregex-group

Regex: Group all blocks of strings starting with a given character


I have in javascript a text like

some text
- list
- list
- list
more text
- another list
- another list
- another list

How can I match all the lines that starts with the character - until a line doesn't start with that character?
For example, given the text above I need to match the two groups

- list
- list
- list

and

- another list
- another list
- another list

I tried with the regex /^(-.*\n-.*|$)/gm, but works only if the list contains an even number of elements, while I need it to work for an arbitrary number of elements.


Solution

  • Currently you only match 2 times a hyphen (an even number) and you are not repeating that match.

    You could match a single line starting with a hyphen, and then optionally repeat following lines staring with a newline and a hyphen.

    ^-.*(?:\n-.*)*
    

    Regex demo


    A bit more extensive pattern making use of a capture group:

    ^[^-\s].*\n(-\s+[^\s-].*(?:\n-\s+[^\s-].*)*)
    

    The pattern matches:

    • a leading text on the previous line that does not start with a hyphen
    • a whitespace character after each leading -
    • at least a single non whitespace character other than - as a value for every item

    Regex demo