1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[JavaScript] trim()メソッドは文字列両端の改行文字 \n や \r を削除する。

Last updated at Posted at 2021-01-02

String.trim()メソッドは文字列両端の空白を削除する。

この空白は、文字通りの空白であるスペースやタブだけでなく、改行文字\r, \n, \r\nも含む。


const memo = 'とんこつラーメンか、\r\n醤油とんこつラーメンで';


const array1 = memo.split('\r')
console.log(array1);
// [ 'とんこつラーメンか、', '\n醤油とんこつラーメンで' ]


const array2 = memo.split('\n').map(m => m.trim())
console.log(array2);
// [ 'とんこつラーメンか、', '醤油とんこつラーメンで' ]


const array3 = memo.split('\r').map(m => m.trim())
console.log(array3);
// [ 'とんこつラーメンか、', '醤油とんこつラーメンで' ]


const array4 = memo.split('').map(m => m.trim())
console.log(array4);
// [ 'とんこつラーメンか', '醤油とんこつラーメンで' ]  

1
2
0

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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?