0
0

More than 1 year has passed since last update.

JavaScriptで改行前後の文字列をそれぞれ抽出したい

Posted at

結論

substrindexOfを組み合わせれば良い

改行前の文字列を抽出する

// 改行前の文字が欲しい
const text = "test1\ntest2";
const text_first_line = text.substr(0, text.indexOf(`\n`));
console.log(`text_first_line=${text_first_line}`);

// 結果
test1

改行後の文字列を抽出する

// 改行後の文字が欲しい
const text = "test1\ntest2";
const text_second_line = text.substr(text.indexOf(`\n`) + 1);
console.log(`text_second_line=${text_second_line}`);

// 結果
test2

参考

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0