I need to split a multi-line string and group it by a specific regex pattern that repeats throughout the text
Some random filler
in the beginning
of the content
Checking against xyz...
Text goes here
More text
and more.
Checking against abc...
Another set of text
with more details
Based on the above example, I want to create groups based on the text following the phrase Checking against
(like xyz
and abc
in this case), with each group containing the lines after that term up until the next occurrence.
After grouping, the output should look something like this, making it easy to access the values under each category:
{
xyz: 'Text goes here\nMore text\nand more.'
abc: 'Another set of text\nwith more details'
}
To achieve this, my initial plan is to divide the string into an array of elements separated by newlines, but then I'm facing difficulties in:
- Finding occurrences of "Checking against" and assigning them as keys
- Appending every line until the next occurrence as the value for that key