I understand regex broadly but I hoped somebody could help understand this a bit better. I’m using it to insert a double new line where there was only one before- it almost works, but not quite. I stole this from ‘github flavored markdown’ – it modifies to support new lines more intuitively.
cleaned.gsub!(/^[\w\<][^\n]*\n+/) do |x|
x =~ /\n{2}/ ? x : (x.strip!; x << " \n")
end
/^– From beginning of the string:[\w\<]– Match any latter character or Open angle bracket. After that:[^\n]*– There should be any non-new line characters, and\n+/– at the end should be one or many new lines.So then on every match code replaces with:
If matched code contains two new-lines, then keep it untouched.
Otherwise strip it an add space and new line at the end.