2
0

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 5 years have passed since last update.

separatorを含めたsplit

Last updated at Posted at 2019-07-03

separatorを引数にsplitすると...

通常下記のようにsplitに引数を渡し、分割した結果を取得します。引数に指定した値で分割しますが指定した文字は含まない値を返します。
業務でどうしてもseparatorの文字も含めて配列を取得したい場合があったので備忘メモ

'abcd1efg2hijk'.split(/[0-9]/);
// 結果 => [ 'abcd', 'efg', 'hijk' ]

1と2も値の中に含めたい場合は肯定先読みを利用します。

'abcd1efg2hijk'.split(/(?=[0-9])/)
// 結果 => [ 'abcd', '1efg', '2hijk' ]

これでseparator文字を含めた文字列の分割が可能です。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?