Regular Expression can be used in the standard
match function to extract the first line of a multiline string, e.g. content of a source file.
"This is the first line.\n This is the second line.".match(/^.*$/m); // => "This is the first line."
^: begin of line
.: any char (.), 0 or more times
$: end of line.
m: multiline mode (. acts like a \n too)
: get first position of array of results
It works with Windows’ CRLF end of line sequence too.
"This is the first line.\r\n This is the second line.".match(/^.*$/m); // => "This is the first line."
Creator and author at DEPHONY. I develop open-source/free software and write technical tutorials that are straight to the point to save readers’ time to learn and apply. Support my work by becoming a Patron 🙂