どうも、ちょげ(@chogetarou)です。
文字列(string)を改行区切りで分割して配列(array)に変換する方法を紹介します。
方法

文字列(string)を改行区切りで分割して配列(array)に変換するには、split()を使います。
まず、文字列からsplit()を呼び出します。
そして、split()の引数に「/[\r\n]+/
」を指定します。
const result = text.split(/[\r\n]+/);
上記のsplit()は、呼び出した文字列を改行区切りで分割した配列を結果として返します。
使用例
const text = "one\ntwo\r\nthree\nfour\rfive";
const result = text.split(/[\r\n]+/);
console.log(result);
出力:
[ 'one', 'two', 'three', 'four', 'five' ]
コメント