What is the best way to split a long string of text into individual lines? And why does this code snippet return "line1" twice?
/^(.*?)$/mg.exec('line1\r\nline2\r\n');
["line1", "line1"]
By enabling the multi-line modifier, I made sure that the ^
and $
characters match the beginning and end of each line. The global modifier was also turned on so that all lines are captured.
The reason for using a regex split instead of String.split
is because I need to account for both Linux (\n
) and Windows (\r\n
) line endings.