summaryrefslogtreecommitdiff
path: root/splitNewLines.go
blob: 7ee74427a8d7a37976b75b329aa715468918c30b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package shell

import "regexp"

// splits strings. should work all the time
// A string with mixed line endings, including old Mac style (\r)
func SplitNewLines(input string) []string {
	// This regex matches a carriage return and optional newline, OR just a newline.
	// This covers \r\n, \n, and \r.
	re := regexp.MustCompile(`\r\n?|\n|\r`)

	// The -1 means there is no limit to the number of splits.
	lines := re.Split(input, -1)

	// Output: ["line one" "line two" "line three" "line four"]
	return lines
}