Q:

Ruby split keep the delimiter before the string

I have the following string : 
a = '% abc \n %% abcd \n %% efgh\n '

I would like the ouput to be 
['% abc \n', '%% abcd \n', '%% efgh \n']

If I have 
b = '%% abc \n %% efg \n %% ijk \n]

I would like the output to be 
['%% abc \n', '%% efg \n', '%% ijk \n']

I use b.split('%%').collect!{|v| '%%' + v } and it works fine for case 2.
but it doesn't work for case 1.
I saw some post of using 'scan' or 'split' to keep the delimiter if its after the string 
For example : 'a; b; c' becomes ['a;', 'b;' ,'c']

But I want the opposite ['a', ';b', ';c']

There need not be space between \n and %% since \n depicts a new line.
A solution i made was
sel = '% asd \n %% asf sdaf \n %% adsasd asdf asd asf ';
delimiter = '%%';
indexOfPercent = test_string.index("%%")

if(indexOfPercent == 0)
    result = (test_string || '').split(delimiter).reject(&:empty?).collect! {|v| delimiter + v}
else
    result =  (test_string.slice(test_string.index("%%")..-1) || '').split(delimiter).reject(&:empty?).collect! {|v| delimiter + v}
    result.unshift(sel[0.. indexOfPercent-1])
end

A:

(?<=\\n)\s*(?=%%)

You can split on the space using lookarounds.See demo.
https://regex101.com/r/fM9lY3/7

